数据网格项目源的 MVVM 属性

发布于 2024-09-05 10:11:10 字数 688 浏览 5 评论 0原文

我有一个数据网格,其 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 并删除列表中的项目或添加新项目。

当我像这样设置时,如何访问项目源?

非常感谢。

I have a datagrid whose itemsSource is bound to a multiconverter which uses a converter.

<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>

This is good because the itemsSource of the grid is updated whenever the combobox or datepicker changes value.

The problem I now have is that in my ViewModel I want to be able to access the ItemSource of my datagrid and either remove items for the list or add new ones.

How do I get access to the itemssource when I have it set up like this?

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下壹個目標 2024-09-12 10:11:10

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

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

希望这有帮助。

How about having three properties in the 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;}
 }

Then bind the datepicker, combobox and datagrid to the respective values.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文