我有一个数据网格,其 itemsSource 绑定(bind)到使用转换器的多重转换器。
<toolkit:DataGrid AutoGenerateColumns="False">
<toolkit:DataGrid.ItemsSource>
<MultiBinding Converter="{StaticResource ProfileConverter}">
<Binding ElementName="ComboBoxProfiles" Path="SelectedValue" />
<Binding ElementName="DatePickerTargetDate" Path="SelectedDate" />
</MultiBinding>
</toolkit:DataGrid.ItemsSource>
这很好,因为只要组合框或日期选择器更改值,网格的 itemsSource 就会更新。
我现在遇到的问题是,在我的 ViewModel 中,我希望能够访问数据网格的 ItemSource 并删除列表中的项目或添加新项目。
当我像这样设置时,如何访问项目源?
非常感谢。
最佳答案
在 ViewModel 中拥有三个属性怎么样:
public DateTime? SelectedDate
{
get{return _selectedDate;}
set
{
_selectedDate = value;
UpdateItemsSource();
OnPropertyChanged("SelectedDate");
}
}
public object SelectedComboBoxValue
{
get{return _selectedComboBoxValue;}
set
{
_selectedComboBoxValue= value;
UpdateItemsSource();
OnPropertyChanged("SelectedComboBoxValue");
}
}
private void UpdateItemsSource()
{
_itemsSource = //Some fancy expression based on the two fields.
OnPropertyChanged("ItemsSource");
}
public IEnumerable ItemsSource
{
get{return _itemsSource;}
}
然后将日期选择器、组合框和数据网格绑定(bind)到各自的值。
希望这有帮助。
关于wpf - 数据网格项目源的 MVVM 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3021940/