如何将ValueConverter源与样式中的目标单元格绑定 [英] How to bind a ValueConverter source to target cell from style
问题描述
嗨!
我有一些带有一些列的DataGrid。我需要根据它们的值为部分列着色。因此我做了一个IValueConverter实现。我在样式中添加了一个背景属性setter:
< 样式 x:键 = CenterCellStyle TargetType < span class =code-keyword> = {x:Type DataGridCell} >
< Setter 属性 = 背景 值 = {Binding ??? ,Converter = {StaticResource ResourceKey = A larmConverter}} / >
...
受影响的单元格如下所示:
< DataGridTextColumn 标题 = 黑色 Binding = {Binding [1] } 宽度 = 100 CellStyle = {StaticResource CenterCellStyle} / >
如果我写代替 ??? 绑定到网格的类的任何属性名称,该行中所有受影响的单元格将根据该属性进行着色。但我需要根据它自身的价值为每个细胞着色。
我应该用什么来代替 ??? ?
我发现此处 [ ^ ]以下表达看起来很有希望: {Binding Text,RelativeSource = {RelativeSource Self}}
,但它不起作用 - 我想因为DataGridTextColumn没有Text属性,我应该绑定到绑定到单元格的字段,但是有一些相对引用。但不知道怎么做。
请指教。谢谢。
我自己解决了这个问题:
< setter property = 背景 value = {Binding RelativeSource = {RelativeSource Self} ,Path = Content.Text ,Converter = {StaticResource ResourceKey = AlarmConverter}} / >
...并且传递给转换器的对象是细胞。这就是我所需要的。
[为什么我不能删除我的解决方案 - 我不能从一开始就正确添加我自己的解决方案,不仅在发布了其他内容之后?]
< setter property = 背景 value < span class =code-keyword> = {Binding RelativeSource = {RelativeSource Self},Path = Content.Text ,Converter = { StaticResource ResourceKey = AlarmConverter}} / > 跨度>
Hi!
I have a DataGrid with some columns. I need to color part of the columns based on their value. Thus I have made an IValueConverter implementation. I added a background property setter to the style:
<Style x:Key="CenterCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="{Binding ???, Converter={StaticResource ResourceKey=AlarmConverter}}" />
...
The affected cells look like this:
<DataGridTextColumn Header="Black" Binding="{Binding [1]}" Width="100" CellStyle="{StaticResource CenterCellStyle}"/>
If I write in place of ??? any property name of the class bound to the grid, all affected cells in the row will be colored based on that property. But I need to color every cell based on it''s own value.
What should I write in place of ??? ?
I have found here[^] the following expression that looked promising: {Binding Text, RelativeSource={RelativeSource Self}}
, but it is not working - I suppose because DataGridTextColumn has no Text property, and I should bind to the field bound to the cell, but with some relative reference. But don''t know how.
Please advice. Thank you.
I have solved it myself:
<setter property="Background" value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource ResourceKey=AlarmConverter}}" />
... and the object passed to the converter is the string in the cell. And that is what I needed.
[Why can''t I delete my solution - and can''t I add my own solution properly right from the start, not only after an other is posted?]
<setter property="Background" value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource ResourceKey=AlarmConverter}}" />
这篇关于如何将ValueConverter源与样式中的目标单元格绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!