开发者

How do I bind the background of a data grid row to specific color?

开发者 https://www.devze.com 2023-04-03 02:56 出处:网络
I have a observable collection that binds to a data grid. I also have in the view model a colo开发者_如何转开发r property and I want to bind the background of each row in the data grid to the color pr

I have a observable collection that binds to a data grid. I also have in the view model a colo开发者_如何转开发r property and I want to bind the background of each row in the data grid to the color property on the vm.


You can bind the Background in the RowStyle for DataGrid

<DataGrid ...>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="{Binding MyBackground}"/>
        </Style>
    </DataGrid.RowStyle>
    <!-- ... -->
</DataGrid>

This will work if MyBackground is a Brush. You mention in your question that you have a Color, if this is the case you can use this instead

<Setter Property="Background">
    <Setter.Value>
        <SolidColorBrush Color="{Binding MyColor}"/>
    </Setter.Value>
</Setter>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号