Reputation: 19356
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.
Upvotes: 1
Views: 3236
Reputation: 169200
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>
Upvotes: 2
Reputation: 1253
I think you will need own DataTemplate for each category and DataTemplateSelector to assign DataTemplates based on category type.
Upvotes: -1
Reputation: 35681
introduce a property which will get/set the correct state based on category:
public long? State
{
get
{
if (IDCategory == 1) return IDState01;
if (IDCategory == 2) return IDState02;
return null;
}
set
{
if (IDCategory == 1) IDState01 = value;
else if (IDCategory == 2) IDState02 = value;
}
}
in DataGird bind column to a new property:
<DataGridTextColumn Header="State" Binding="{Binding State}"/>
Upvotes: 2