如何通知viewmodel集合模型类上的属性已更改

时间:2013-06-21 20:31:38

标签: wpf mvvm datagrid checkbox

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

这是我模特中的课程......

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

这是我的viewmodel中的集合......

    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>

2 个答案:

答案 0 :(得分:0)

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

在您的VM构造函数中说:

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
}

应该是它。

答案 1 :(得分:0)

在Xaml中: 添加以下namspace ..

    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
    }

希望这会对你有所帮助。