【问题标题】:How to notify viewmodel collection that property on model class has changed如何通知 viewmodel 集合模型类的属性已更改
【发布时间】:2023-04-02 23:25:01
【问题描述】:

我有一个类,它有一个名为IsChecked 的布尔属性。 我的视图模型中存在此类的集合。我已将视图中的数据网格绑定到此集合。当视图中的复选框发生更改时,我需要在视图模型中调用一个方法。我已经在类上实现了INotifyPropertyChanged,当我选中该框时它正在触发,但我不知道如何在我的视图模型中调用该方法。

这是我模型中的类...

public class AccountComponent : INotifyPropertyChanged
{
    public string Name { get; set; }
    public decimal Amount { get; set; }

    private bool _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            _isChecked = value;
            NotifyPropertyChanged("IsChecked");
        }
    }

    public bool Enabled { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

这是我的视图模型中的集合...

    private ObservableCollection<AccountComponent> _accountComponents;
    private string _accountStatus;

    public ObservableCollection<AccountComponent> AccountComponents
    {
        get { return _accountComponents; }
        set
        {
            _accountComponents = value;
            NotifyPropertyChanged("AccountComponents");
            CalculateComponentTotal();
        }
    }

这是视图中的我的 XAML...

<DataGrid ItemsSource="{Binding AccountComponents}" AutoGenerateColumns="False" Margin="5">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <CheckBox IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" IsEnabled="{Binding Enabled}"/>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Binding="{Binding Name}" Header="Component" Width="*" IsReadOnly="True" ElementStyle="{DynamicResource TextBlock-Sketch}"/>
                <DataGridTextColumn Binding="{Binding Amount,StringFormat={}{0:C}}" IsReadOnly="True" Header="Charge" ElementStyle="{DynamicResource TextBlock-Sketch}">
                    <DataGridTextColumn.CellStyle>
                        <Style>
                            <Setter Property="TextBlock.TextAlignment" Value="Right"/>
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>

【问题讨论】:

    标签: wpf mvvm datagrid checkbox


    【解决方案1】:

    由于AccountComponent 实现了INPC,您可以在VM 中观察IsChecked 属性。

    在你的虚拟机构造函数中说:

    AccountComponents = new ObservableCollection<AccountComponent>();
    AccountComponents.CollectionChanged += AccountComponentsOnCollectionChanged;
    
    ...
    
    private void AccountComponentsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) {
      if (args.NewItems != null && args.NewItems.Count != 0)
        foreach (AccountComponent account in args.NewItems)
          account.PropertyChanged += AccountOnPropertyChanged;
    
      if (args.OldItems != null && args.OldItems.Count != 0)
        foreach (AccountComponent account in args.OldItems)
          account.PropertyChanged -= AccountOnPropertyChanged;
    }
    
    private void AccountOnPropertyChanged(object sender, PropertyChangedEventArgs args) {
      if (args.PropertyName == "IsChecked")
        // Invoke Your VM Function Here
    }
    

    应该是这样的。

    【讨论】:

      【解决方案2】:

      在 Xaml 中: 添加以下命名空间..

          xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
      

      现在为您在复选框中添加以下代码:

          <CheckBox>
              <i:Interaction.Triggers>
                  <i:EventTrigger EventName="Checked">
                      <i:InvokeCommandAction Command="{Binding CheckedCommand}"/>
                  </i:EventTrigger>
              </i:Interaction.Triggers>
          </CheckBox>
      

      在 ViewModel 中:

       public ICommand CheckedCommand
          {
              get
              {
                  return new DelegateCommand(OnChecked);//Delegate command is the Implemntation of Icommand Interface
              }
          }
      
      public void OnLogin(object param)
          {
              //code for you checked event
          }
      

      希望这会对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-30
        • 2021-04-16
        • 2018-08-20
        • 1970-01-01
        相关资源
        最近更新 更多