WPF DataGrid - 基于行动态绑定DataGridComboBoxColumn

时间:2015-08-12 16:29:27

标签: c# wpf data-binding datagrid datagridcomboboxcolumn

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

我有一个属性PossibleParentObjects,它根据SelectedObject返回List个对象。

我希望此属性填充ComboBox

列中的DataGrid

我该怎么做?

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

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

2 个答案:

答案 0 :(得分:2)

请参阅讨论类似问题的this Stackoverflow question

基本思路如下:

  • 将datagrid列绑定到viewmodel中的单个属性,让我们调用它&#34; ParentObjects&#34;
  • 将数据网格的选定行绑定到viewmodel中的另一个属性。 在该属性的setter中,您应该检索DataGridComboBox列的组合框所需的数据项,并使用它来设置&#34; ParentObjects&#34;属性

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

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

答案 1 :(得分:1)

这是最终为我工作的代码。这是“答案”,但Nepdev的答案让我来到这里。希望这会帮助其他人尝试做类似的事情。

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