-2

I have a 2 column WPF Datagrid that is bound to an ObservableCollection of people objects. One of the grid's columns is a dropdownlist displaying (correctly) the gender - Male or Female.

What I want to do is dynamically display choices in another dropdownlist in the second column (i.e.Col2) based on the bound value of Male or Female.

I don't see a OnRowBound event; but it seems like I'll need to swap my itemsource on Col2, based on the gender column, to produce the values for the dropdownlist in Col2 column {per row}.
Does this sound doable?

fjr_nj
  • 17
  • 4

2 Answers2

0

You can use sample code below to perform such a task. It assumes that you have some kind of Gender property (enum) and collection for Female and Male items that should appear in your column. It is not complete - you should add selected items bindings, displaymemberpath, etc. But I think it's enough for you to get the idea:

<DataGrid ItemsSource="{Binding SomeCollection}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Gender}"/>
        <DataGridComboBoxColumn>
            <DataGridComboBoxColumn.ElementStyle>
                <Style TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Gender}" Value="{x:Static enums:Gender.Female}">
                            <Setter Property="ItemSource" Value="{Binding FemaleItems}"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Gender}" Value="{x:Static enums:Gender.Male}">
                            <Setter Property="ItemSource" Value="{Binding MaleItems}"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGridComboBoxColumn.ElementStyle>
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
</DataGrid>
Ivan Zub
  • 785
  • 3
  • 13
0

Thanks Ivan,

Here is a complete example I was looking for.

http://sekagra.com/wp/2013/04/dynamic-itemssource-for-combobox-in-a-datagrid/

fjr_nj
  • 17
  • 4