在WPF datagrid中选择行时,设置datagridhypercolumn中所选单元格的前色 [英] Setting the forecolor of selected cell in datagridhypercolumn when a row is selected in WPF datagrid

查看:99
本文介绍了在WPF datagrid中选择行时,设置datagridhypercolumn中所选单元格的前色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问我在WPF数据网格中选择一行时如何在DatagridHypercolumn中设置所选单元格的前景色.

有关DatagridText列,单元格内容的前景色是白色默认,当选择的行.

但是对于DatagridHyperlink列,前景色仍然保持为蓝色.

我需要通过代码进行设置.

我尝试了以下操作,但这无济于事.

   Style rowstyle = new Style(typeof(System.Windows.Controls.DataGridCell));

   Trigger trigger = new Trigger { Property = System.Windows.Controls.DataGridCell.IsSelectedProperty, Value = true };

   trigger.Setters.Add(new Setter(System.Windows.Controls.DataGridCell.ForegroundProperty, Brushes.White));

   rowstyle.Triggers.Add(trigger);

   System.Windows.Controls.DataGridHyperlinkColumn hyperlinkCol = new System.Windows.Controls.DataGridHyperlinkColumn { CellStyle = rowstyle };

   this.datagrid.Columns.Add(hyperlinkCol);

解决方案

这就是我要解决的问题的方式.

开始遍历视觉树,直到到达事物"目录.您正在寻找...然后,您可以将对象投射到任何物体上,并进行相应的操作.

有时,我发现这些数据网格组件和控件的默认外观会因其抽象类型而变得难以理解...什么?在WPF中,有不同类型的控件,例如内容"控件.但是你不知道 总是知道在默认模板中该内容是如何构成的.例如,您是否正在处理带有超链接的网格?

查看这些内容的最佳方法是在Expression Blend中...,因为它们清楚地显示了模板化的部分. Blend不过是向您显示可视化树的工具.但是一旦您了解了视觉树,您就可以真正创建一个数据模板 对于该控件...问题是您必须知道名称才能调用/覆盖样式.原因:所有样式都需要目标类型.如果您不知道名称,则无法覆盖模板....数据网格具有一些奇怪的模板部分 名称.

 


Hi,

Can you please advise me how to set the forecolor of selected cell in DatagridHypercolumn when a row is selected in WPF datagrid.

For DatagridText columns, the forecolor of the cell content is White by default, when a Row is selected.

But for DatagridHyperlink column, the forecolor still stays as Blue.

I need to set this through code.

I tried the following, but that does not help.

   Style rowstyle = new Style(typeof(System.Windows.Controls.DataGridCell));

   Trigger trigger = new Trigger { Property = System.Windows.Controls.DataGridCell.IsSelectedProperty, Value = true };

   trigger.Setters.Add(new Setter(System.Windows.Controls.DataGridCell.ForegroundProperty, Brushes.White));

   rowstyle.Triggers.Add(trigger);

   System.Windows.Controls.DataGridHyperlinkColumn hyperlinkCol = new System.Windows.Controls.DataGridHyperlinkColumn { CellStyle = rowstyle };

   this.datagrid.Columns.Add(hyperlinkCol);

解决方案

This is how I would tackle this problem.

Start walking the Visual tree until you get to the "thing" you are looking for...  Then you can cast the object to what ever it is and manipulate it accordingly.

Sometimes, I find that the default visuals for these datagrid components and controls for that matter to be obsured by their abstract type... What?  In WPF there are different types of controls for example Content controls.  But what you don't always know is how that content is constucted in the default templates.  So for example are you dealing with a grid with a hyperlink in it? 

The best way to see these is in Expression Blend... because they show the templated parts clearly.  Blend is nothing more than a tool that shows you the Visual tree.  But once you understand the visual tree you can actually create a datatemplate for that control... Problem is you have to know the name in order to invoke/override the style.  Reason: All styles require a target type....  If you don't know the name you can't override the template....Data Grids have some weird template part names.

 


这篇关于在WPF datagrid中选择行时,设置datagridhypercolumn中所选单元格的前色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆