Change DataGrid cell colour based on values

前端 未结 8 1476
栀梦
栀梦 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条回答
  • 2020-11-22 09:32
            // Example: Adding a converter to a column (C#)
            Style styleReading = new Style(typeof(TextBlock));
            Setter s = new Setter();
            s.Property = TextBlock.ForegroundProperty;
            Binding b = new Binding();
            b.RelativeSource = RelativeSource.Self;
            b.Path = new PropertyPath(TextBlock.TextProperty);
            b.Converter = new ReadingForegroundSetter();
            s.Value = b;
            styleReading.Setters.Add(s);
            col.ElementStyle = styleReading;
    
    0 讨论(0)
  • 2020-11-22 09:34

    Just put instead

    <Style TargetType="{x:DataGridCell}" >
    

    But beware that this will target ALL your cells (you're aiming at all the objects of type DataGridCell ) If you want to put a style according to the cell type, I'd recommend you to use a DataTemplateSelector

    A good example can be found in Christian Mosers' DataGrid tutorial:

    http://www.wpftutorial.net/DataGrid.html#rowDetails

    Have fun :)

    0 讨论(0)
提交回复
热议问题