简体   繁体   English

文本块在数据网格 WPF 中选择了前景色

[英]Textblock selected foreground color in datagrid WPF

I have created a datagrid in WPF...我在 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?这个问题有什么解决办法吗?

Add DataTrigger to the DataTemplate triggers collection that would change foreground based on selected state of the row.DataTrigger添加到DataTemplate触发器集合,该集合将根据行的选定 state 更改前景。 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.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM