WPF DataGrid - 根据行动态绑定DataGridComboBoxColumn [英] WPF DataGrid - Dynamically Bind DataGridComboBoxColumn based on row

查看:229
本文介绍了WPF DataGrid - 根据行动态绑定DataGridComboBoxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ObservableCollection 的对象。这些对象显示在 DataGrid &一个`SelectedObject

I have an ObservableCollection of objects. These object are displayed in a DataGrid & a `SelectedObject

我有一个属性 PossibleParentObjects 返回一个列表基于SelectedObject的对象。

I have a property PossibleParentObjects that returns a List of Objects based on the SelectedObject.

我想让这个属性填充一个 ComboBox DataGrid的列

I would like to have this property populate a ComboBox residing in a column of the DataGrid

我该怎么做?

这是我到目前为止...显然不工作:

Here is what I have so far... Obviously not working:

    <DataGrid   Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
            AlternatingRowBackground="AntiqueWhite" AlternationCount="2"
            ItemsSource="{Binding AllObjects, UpdateSourceTrigger=PropertyChanged}"
            SelectedItem="{Binding SelectedObject}"
            CanUserAddRows="True" CanUserDeleteRows="True"
            AutoGenerateColumns="False">

    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name, Mode=TwoWay}" Header="Name"/>
        <DataGridTextColumn Binding="{Binding Abbr, Mode=TwoWay}" Header="Abbreviation"/>
        <DataGridComboBoxColumn Header="Parent Object" Width="120" 
            SelectedItemBinding="{Binding Path=Id, UpdateSourceTrigger=PropertyChanged}"
            DisplayMemberPath="Name"
            ItemsSource="{Binding Path=AllObjects, 
              RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">                    
        </DataGridComboBoxColumn>
        <DataGridTextColumn Binding="{Binding Desc, Mode=TwoWay}" Header="Description"/>
    </DataGrid.Columns>

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="CurrentCellChanged">
            <i:InvokeCommandAction Command="{Binding DataGridChanged}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

</DataGrid>


推荐答案

请参阅这个Stackoverflow问题,讨论了类似的问题。

see this Stackoverflow question which discusses a similar problem.

基本思想是:


  • 将datagrid列绑定到viewmodel中的一个属性,让我们称之为ParentObjects

  • 将datagrid的选定行绑定到viewmodel中的另一个属性。
    在该属性的setter中,您应该检索DataGridComboBox列的组合框所需的数据项,并使用它来设置ParentObjects属性

这样,每当用户更改他想要查看的行时,它将自动检索正确的对象并填充组合框列。
换句话说,您不会使用组合框的源来检索它,但是在更改所选行时检索。你必须这样做 - 属性系统不允许参数。

This way, whenever the user changes the row he wants to see, it will automatically retrieve the correct objects and populate the combobox column. In other words, you don't retrieve it using the source of the combo box but you retrieve upon change of the selected row. You'll have to do this - the property system does not allow parameters.

我知道这是我在这里给出的一般描述,而不是代码,但我认为你'将获得要点。

I know it is a general description I give here, not code, but I think you'll get the gist.

这篇关于WPF DataGrid - 根据行动态绑定DataGridComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆