wpf - Use IValueConverter to Color DataGridCell - Stack Overflow

I have a DataGrid in my WPF-Application. There i have to color the rows depending on the data in the ro

I have a DataGrid in my WPF-Application. There i have to color the rows depending on the data in the row. For this i wrote a ValueConverter and it works. For each possible color i have an DataTrigger in my XAML:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ColorCellValueConverter}}" Value="Case1">
    <Setter Property="Background" Value="Yellow" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ColorCellValueConverter}}" Value="Case2">
    <Setter Property="Background" Value="LightGreen" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ColorCellValueConverter}}" Value="Case3">
    <Setter Property="Background" Value="Orange" />
</DataTrigger>

My question is: is it possible to make this better? Is there the possibility to call the converter once and the decide how to color (like a switch-statement)? Or it is possible to bind the return-value of the converter to the setter? So i could return the color und it would also work. Something like this:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ColorCellValueConverter}}">
    <Setter Property="Background" Value=CONVERTER_RETURN_VALUE />
</DataTrigger>

Thanks for your help and ideas!

I have a DataGrid in my WPF-Application. There i have to color the rows depending on the data in the row. For this i wrote a ValueConverter and it works. For each possible color i have an DataTrigger in my XAML:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ColorCellValueConverter}}" Value="Case1">
    <Setter Property="Background" Value="Yellow" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ColorCellValueConverter}}" Value="Case2">
    <Setter Property="Background" Value="LightGreen" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ColorCellValueConverter}}" Value="Case3">
    <Setter Property="Background" Value="Orange" />
</DataTrigger>

My question is: is it possible to make this better? Is there the possibility to call the converter once and the decide how to color (like a switch-statement)? Or it is possible to bind the return-value of the converter to the setter? So i could return the color und it would also work. Something like this:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ColorCellValueConverter}}">
    <Setter Property="Background" Value=CONVERTER_RETURN_VALUE />
</DataTrigger>

Thanks for your help and ideas!

Share Improve this question asked Jan 31 at 10:20 user8527410user8527410 4931 gold badge4 silver badges12 bronze badges 2
  • 4 Why the DataTriggers? The Converter could directly return a Brush. Background="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ColorCellValueConverter}}". – Clemens Commented Jan 31 at 10:58
  • Clemens, that‘s it! It‘a so easy. Thanks a lot!!!! – user8527410 Commented Feb 1 at 16:04
Add a comment  | 

1 Answer 1

Reset to default 0

As Clemens wrote binding the converter directly on the background is the solution. Works great! Thanks a lot.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745269231a4619642.html

相关推荐

  • wpf - Use IValueConverter to Color DataGridCell - Stack Overflow

    I have a DataGrid in my WPF-Application. There i have to color the rows depending on the data in the ro

    3小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信