Search code examples
wpfxamldatagridwpfdatagrid

Conditional binding in column of datagrid


I have a DataGrid which itemsSource is an ObservableCollection<MyType>. This type has this properties:

long ID;
long IDCategory;
long? IDState01;
long? IDEstate02;
long? IDEste03;

I have 3 categories, if it is category 1, IDState01 is not null and the others states are null. If category is 2, the IDState02 is not null and the others are null and son on.

I have a state column in my DataGrid, which value depends on the category. So I would like to binding to the correct property depending on the category, so if the category is 1, it would bind property state01, if the category is 3 it would bind property state02 and so on.

I think that my DataGrid would be something like that:

<DataGrid HorizontalAlignment="Stretch" Margin="5,5,5,5" VerticalAlignment="Stretch">
    <DataGrid.Columns>
        <DataGridTextColumn Header="State">
            <!--Something here, perhaps a datatrigger.-->
        </DateGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

Thanks.

EDIT: I would like to do it in XAML if it is possible, instead of using a converter.


Solution

  • I would like to do it in XAML if it is possible ...

    You could use a DataGridTemplateColumn:

    <DataGridTemplateColumn Header="State">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock>
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IDCategory}" Value="1">
                                    <Setter Property="Text" Value="{Binding IDState01}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding IDCategory}" Value="2">
                                    <Setter Property="Text" Value="{Binding IDEstate02}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding IDCategory}" Value="3">
                                    <Setter Property="Text" Value="{Binding IDEste03}" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
        <DataGridTemplateColumn.CellEditingTemplate>
            <DataTemplate>
                <TextBox>
                    <TextBox.Style>
                        <Style TargetType="TextBox">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IDCategory}" Value="1">
                                    <Setter Property="Text" Value="{Binding IDState01}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding IDCategory}" Value="2">
                                    <Setter Property="Text" Value="{Binding IDEstate02}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding IDCategory}" Value="3">
                                    <Setter Property="Text" Value="{Binding IDEste03}" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>
            </DataTemplate>
        </DataGridTemplateColumn.CellEditingTemplate>
    </DataGridTemplateColumn>