如何通过绑定 UWP C# 中该行中的列值来绑定 controls:DataGrid 中行的颜色?

How to bind the color of a row in controls:DataGrid by binding with the value of a column in that row in UWP C#?

对于某行某列的特定值,如何在UWP C#中更改行背景色>

我已经使用 controls:DataGrid 在 UWP 中显示 DataTable 的值。我正在尝试根据 DataTable 的 "Color" 列中的值绑定行的颜色。如果 "Color" 列是一行的 "Red",我必须设置行背景 "Red",否则为绿色。

<controls:DataGrid.RowStyle>
    <Style TargetType="controls:DataGridRow">
        <Setter Property="Background" Value="{Binding ColumnForColorInDataTable}" />
    </Style>
</controls:DataGrid.RowStyle>

代码部分来自我的 xaml 文件。

UWP中的

XAML不同于WPF,在Setter中写入绑定内容不生效。目前DataGrid属于社区控件,和WPF的DataGrid还有差距。为实现您的目标,我建议使用 ListView 以获得更高程度的个性化。

但是关于更改行背景颜色的目标。 DataGrid 目前没有直接的方法来做到这一点。不过还是有Hack的方法可以达到你的目的。

DataGrid 的每一行都由多个单元格组成。精确控制一行很难,但我们可以控制每个单元格的性能。

假设我们有一个 Person class 具有 NameColumnForColorInDataTable 属性。

<Page
    ...
    >
    <Grid>
        <controls:DataGrid AutoGenerateColumns="False" ItemsSource="{x:Bind PersonCollection}">
            <controls:DataGrid.Columns>

                <controls:DataGridTemplateColumn
                    Header="Name"
                    Width="SizeToHeader">

                    <controls:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate x:DataType="Person">
                            <Border Background="{Binding ColumnForColorInDataTable}">
                                <TextBlock Text="{Binding Name}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Border>
                        </DataTemplate>
                    </controls:DataGridTemplateColumn.CellTemplate>

                </controls:DataGridTemplateColumn>

                <!-- Other Columns-->

            </controls:DataGrid.Columns>
        </controls:DataGrid>
    </Grid>
</Page>

这样,让一排单元格保持相同的颜色,变相达到你的目的。

此致。