Stas Petrov
Stas Petrov

Reputation: 325

WPF Fire PropertyChanged when ui update with mvvm

I am trying to implement mvvm in my application. I have bindings to commands, and i need to fire event when user input some data to textbox for force update CanExecute in command My viewmodel

public class AddEditWindowViewModel: INotifyPropertyChanged
 {
    public event PropertyChangedEventHandler PropertyChanged;
    ......
    public ConfirmCommand ConfirmCommand { get; set; }
    public string Path{ get; set; }

   public AddEditWindowViewModel()
    {
        WindowTitle = "Add new item";
        ConfirmCommand = new ConfirmCommand(this);
    }

   [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

My command

public class ConfirmCommand: ICommand
{
    private AddEditWindowViewModel _model;

    public ConfirmCommand(AddEditWindowViewModel model)
    {
        _model = model;
        _model.PropertyChanged += ModelOnPropertyChanged;
    }

    private void ModelOnPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        if (args.PropertyName.Equals("Path"))
        {
            CanExecuteChanged?.Invoke(this, EventArgs.Empty);
        }
    }

    public bool CanExecute(object parameter)
    {
        return !string.IsNullOrEmpty(_model.Path);
    }

    public void Execute(object parameter)
    {
        .....
    }

    public event EventHandler CanExecuteChanged;
}

My view

 <Window x:Class="NameSpace.Views.AddEditWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="clr-namespace:NameSpace.ViewModels"
    mc:Ignorable="d"
    Title="{Binding Path=WindowTitle}" 
    Height="200" 
    Width="500"
    WindowStyle="SingleBorderWindow">
<Window.DataContext>
    <vm:AddEditWindowViewModel />
</Window.DataContext>
.....
        <TextBox Name="Path" Text="{Binding Path=Path, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="2" Grid.Column="0" Grid.Row="1"></TextBox>
.....
Button IsDefault="True" Command="{Binding Path=ConfirmCommand}" Margin="5" HorizontalAlignment="Right" Width="100" Grid.Row="2" Grid.Column="0" Content="Ok"></Button>
    <Button IsCancel="True" Margin="5" HorizontalAlignment="Left" Width="100" Grid.Row="2" Grid.Column="1" Content="Cancel"></Button>
.....

When textbox updated by user in ui, PorpertyChanged event in ConfirmCommand doesn't firing. Why? Where i got wrong?

Upvotes: 0

Views: 1329

Answers (3)

What for are you implementing such things like: INotifyPropertyChanged and ConfirmCommand by yourself? There are a lot of framework where this was done for you.. And many other things for MVVM approach like EventToCommand, ViewModelLocator, Messenger. For example https://msdn.microsoft.com/en-us/magazine/5eafc6fc-713a-4461-bc2b-469afdd03c31?f=255&MSPPError=-2147217396

Upvotes: 0

Konrad
Konrad

Reputation: 380

You should raise PropertyChanged event if (and only if) a value of your property changes:

private string _path;

public string Path
{
    get 
    {
        return _path;
    }
    set
    {
        if (_path != value)
        { 
            _path = value;
            OnPropertyChanged();
        }
    }
 }

Upvotes: 0

YuvShap
YuvShap

Reputation: 3835

Try to call OnPropertyChanged in your Path property setter:

private string _path;

public string Path
{
    get 
    {
        return _path;
    }
    set
    { 
        _path = value;
        OnPropertyChanged();
    }
}

Upvotes: 1

Related Questions