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

拟墨画扇 提交于 2019-11-28 02:33:45

问题


I have a observable collection that binds to a data grid. I also have in the view model a color property and I want to bind the background of each row in the data grid to the color property on the vm.


回答1:


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>


来源:https://stackoverflow.com/questions/7339509/how-do-i-bind-the-background-of-a-data-grid-row-to-specific-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!