WPF DataGrid -基于行动态绑定DataGridComboBoxColumn

本文关键字:动态绑定 DataGridComboBoxColumn 于行 DataGrid WPF | 更新日期: 2023-09-27 18:07:37

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

我有一个属性PossibleParentObjects,它返回基于SelectedObject的对象的List

我想让这个属性填充位于DataGrid列中的ComboBox

我该怎么做?

这是我到目前为止所做的…

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

WPF DataGrid -基于行动态绑定DataGridComboBoxColumn

参见讨论类似问题的Stackoverflow问题

基本思想如下:

  • 将数据网格列绑定到视图模型中的单个属性,我们将其命名为"ParentObjects"
  • 将数据网格的选定行绑定到视图模型中的另一个属性。在该属性的setter中,您应该检索DataGridComboBox列的组合框所需的数据项,并使用它来设置"ParentObjects"属性

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

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

这是最终为我工作的代码。这是"答案",但正是内德夫的答案让我来到这里。希望这将帮助其他人尝试做类似的事情。

    <DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HeadersVisibility="Column"
      AlternatingRowBackground="AntiqueWhite" AlternationCount="2"
      ItemsSource="{Binding EquipLocations, UpdateSourceTrigger=PropertyChanged}"
      SelectedItem="{Binding SelectedItem}"
      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="Uses Location" Width="120" 
    SelectedValueBinding="{Binding Path=ParentObjectId, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
    SelectedValuePath="Id"
    DisplayMemberPath="Abbr">
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, 
                Path=DataContext.AllPossibleObjects}"/>
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, 
                Path=DataContext.PossibleParentObjects}"/>
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction Command="{Binding DataGridChanged}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </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>