Textblock chose foreground color in datagrid WPF

I created a datagrid in WPF ...
I have defined several custom columns.

<my:DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding HeadC}" /> <TextBlock Text="{Binding HeadCPercent}" Foreground="#FFF05D1D" /> </StackPanel> </DataTemplate> </my:DataGridTemplateColumn.CellTemplate> 

The problem is that when you select a line, the color of the text block of seconds does not change, and it is barely noticeable ...

Any solution to this problem?

+4
source share
1 answer

Add a DataTrigger to the DataTemplate trigger collection, which will change the priority based on the selected row state. As in the following example:

 <DataTemplate> <StackPanel> <TextBlock Text="{Binding HeadC}" /> <TextBlock x:Name="tbPercent" Text="{Binding HeadCPercent}" Foreground="#FFF05D1D"/> </StackPanel> <DataTemplate.Triggers> <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dg:DataGridRow}}}" Value="True"> <Setter Property="Foreground" TargetName="tbPercent" Value="Blue" /> </DataTrigger> </DataTemlate.Triggers> </DataTemplate> 

I took this answer as a basis and adjusted it to your question. I could make a typo in the code, but you should get an idea :). Hope this helps.

+4
source

All Articles