Change DataGrid cell colour based on values

2018-12-31 19:11发布

I have got a WPF datagrid and I want diffrent cell colours according to values. I have got below code on my xaml

Style TargetType="DataGridCell"

but instead of selecting a cell only is selecting all row? What am I missing?

7条回答
看风景的人
2楼-- · 2018-12-31 19:45

To do this in the Code Behind (VB.NET)

Dim txtCol As New DataGridTextColumn

Dim style As New Style(GetType(TextBlock))
Dim tri As New Trigger With {.Property = TextBlock.TextProperty, .Value = "John"}
tri.Setters.Add(New Setter With {.Property = TextBlock.BackgroundProperty, .Value = Brushes.Green})
style.Triggers.Add(tri)

xtCol.ElementStyle = style
查看更多
登录 后发表回答