【问题标题】:MVVM Property for datagrid itemssource数据网格项目源的 MVVM 属性
【发布时间】:2011-03-02 14:31:42
【问题描述】:

我有一个数据网格,其 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?

非常感谢。

【问题讨论】:

    标签: wpf datagrid itemssource


    【解决方案1】:

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

    然后将日期选择器、组合框和数据网格绑定到各自的值。

    希望这会有所帮助。

    【讨论】:

    • 非常感谢。这就是我需要的东西。效果很好。
    猜你喜欢
    • 2013-09-02
    • 2013-06-17
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 2011-03-06
    • 1970-01-01
    相关资源
    最近更新 更多