【问题标题】:WPF Fire PropertyChanged when ui update with mvvm使用 mvvm 更新 ui 时 WPF Fire PropertyChanged
【发布时间】:2016-11-13 07:24:18
【问题描述】:

我正在尝试在我的应用程序中实现 mvvm。我有命令绑定,当用户向文本框输入一些数据以在命令中强制更新CanExecute 时,我需要触发事件 我的视图模型

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));
    }
}

我的命令

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;
}

我的看法

 <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>
.....

当用户在 ui 中更新文本框时,ConfirmCommand 中的 PorpertyChanged 事件不会触发。为什么? 我哪里错了?

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    尝试在 Path 属性设置器中调用 OnPropertyChanged:

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

    【讨论】:

      【解决方案2】:

      当(且仅当)您的属性值发生更改时,您应该引发 PropertyChanged 事件:

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

      【讨论】:

        【解决方案3】:

        你为什么要自己实现这样的事情:INotifyPropertyChangedConfirmCommand?有很多框架可以为您完成此操作。还有许多其他适用于 MVVM 方法的东西,例如 EventToCommand、ViewModelLocator、Messenger。例如https://msdn.microsoft.com/en-us/magazine/5eafc6fc-713a-4461-bc2b-469afdd03c31?f=255&MSPPError=-2147217396

        【讨论】:

          猜你喜欢
          • 2014-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多