Change DataGrid cell colour based on values

前端 未结 8 1631
栀梦
栀梦 2020-11-22 08:48

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\"

8条回答
  •  Happy的楠姐
    2020-11-22 09:23

    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
    

提交回复
热议问题