我已经绑定了ObservableCollection到ItemSource到DataGrid,但是,我想通过ViewModel检索(通过setter)单个属性。WPF绑定属性到Datagrid
好听起来令人困惑,所以会解释。在我的ObservableCollection中,我有一个名为“Active”的属性,所以我想在用户点击或关闭DataGrid中的复选框时设置该元素。
所以XAML
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Active, Mode=TwoWay}" HorizontalAlignment="Center"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
而且我想,当没有选中该复选框这触发视图模型的代码或检查
private bool m_Active = false;
public bool Active
{
get { return m_Active; }
set
{
m_Active = value;
OnPropertyChanged("Active");
}
}
但即使双向模式上,这不是”吨。任何原因为什么?
注意:在DataGrid的SelectedItem属性中,我可以得到SelectedRow,所以基本上我想要选择Individual属性!
感谢
datacontext是不同的。复选框的datacontext将是行项目,但您的属性在您的视图模型中。 – Shoe
我明白,这意味着我只能从设置DataContext的行项目中获取单个属性? – user3428422