2

我有一个数据网格,其 itemsSource 绑定到使用转换器的多转换器。

<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 并删除列表的项目或添加新项目。

当我这样设置时,如何访问 itemssource?

非常感谢。

4

1 回答 1

4

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

然后将 datepicker、combobox 和 datagrid 绑定到各自的值。

希望这可以帮助。

于 2010-06-11T10:22:35.777 回答