Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textblock selected foreground color in datagrid WPF

I have 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 a row is slected the seconds textblock color doesnt change and it is hardly visible...

Any solution to this problem ?

like image 630
GorillaApe Avatar asked Jul 04 '11 15:07

GorillaApe


1 Answers

Add DataTrigger to the DataTemplate triggers collection that would change foreground based on selected state of the row. Like 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 made a typo in the code but you should get the idea :). Hope it helps.

like image 60
Anvaka Avatar answered Nov 15 '22 05:11

Anvaka