WPF:如何突出显示一个DataGrid的所有单元格满足条件? [英] WPF: How to highlight all the cells of a DataGrid meeting a condition?
问题描述
我没有这样使用它。我使用它来代表2个集合之间的交集。行表示集合A的对象,列表示集合B中的对象。x,y处的单元格表示来自fn(collectionA [x],collectionB [y])的某个值。
这工作正常。现在我想要查询单元格值。我想要用户能够做的事情,比如突出显示大于0的所有单元格?
如何使用datagrid?
一种方法是创建一个以相应方式行为的数据单元格模板(通过controltemplates.triggers集合)。另一种方式是创建一个数据单元格样式,并使用样式触发器来设置一个给定的绑定具有一定值(通过style.triggers集合)的背景。
或者您可以使用混合方法。
您的数据单元格模板中可能包含一个包含所有内容的边框元素。例如,您可以为该边框元素创建内联样式。
< Border Width =AutoHeight =AutoPadding =6,10,6,10CornerRadius = 0,0,20,20>
< Border.Style>
< Style TargetType ={x:Type Border}>
< Setter Property =BackgroundValue =White/>
< Style.Triggers>
< DataTrigger Binding ={Binding Highlighted}Value =True>
< Setter Property =BackgroundValue ={StaticResource GreenGradientSuccessBrush}/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /Border.Style>
< / Border>
如果这是坐在数据模板中,边框元素的数据文本将是包含的对象,当内容的突出显示属性(假设内容是具有高亮布尔属性的类)时,边框触发器将触发为True。
如果内容是一个整数,你想要触发器,当它大于零时,你将不得不使用
< DataTrigger绑定={Binding,Converter = {StaticResource GreaterThanZeroConverter}}Value =True>
其中GreaterThanZeroConverter是一个合适的值的转换器。
The (wpftoolkit) datagrid generally represents a collection of objects with rows representing each object, and the columns the repective properties.
I am not using it this way. I am using it to represent the "intersection" between 2 collections. Rows represent objects of collection A and columns represent objects in collections B. A cell at x,y represent some value from fn(collectionA[x],collectionB[y]).
This works fine. Now I want to be able to query the cell values. I want the user to be able to do things like highlight all cells with value greater than 0? How do I do this with the datagrid?
One way is to create a data cell template that behaves in a corresponding manner (via controltemplates.triggers collection).
Another way is to create a data cell style and use style triggers to set the background when a given binding has a certain value (via style.triggers collection).
Or you can use a hybrid approach.
Inside your data cell template, you might have a border element containing everything. You can, for example, create an inline style for that border element.
<Border Width="Auto" Height="Auto" Padding="6,10,6,10" CornerRadius="0,0,20,20">
<Border.Style>
<Style TargetType="{x:Type Border}" >
<Setter Property="Background" Value="White" />
<Style.Triggers>
<DataTrigger Binding="{Binding Highlighted}" Value="True">
<Setter Property="Background" Value="{StaticResource GreenGradientSuccessBrush}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
If this is sitting in a data template, the datacontext of the border element will be the contained object, and the border trigger will fire when "Highlighted" property of the content (assuming content is a class with a "Highlighted" boolean property) is "True".
If your content is an integer and you want the trigger to fire when it is greater than zero you will have to use
<DataTrigger Binding="{Binding, Converter={StaticResource GreaterThanZeroConverter}}" Value="True">
where the GreaterThanZeroConverter is a suitable value converter.
这篇关于WPF:如何突出显示一个DataGrid的所有单元格满足条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!