【问题标题】:Programmatically Change Cell Colour以编程方式更改单元格颜色
【发布时间】:2017-06-20 16:06:26
【问题描述】:

我有两个Ints 列表,我需要将它们并排放置到两个垂直列中。如果数字不匹配,我需要突出显示(一个或两个单元格,哪个都无所谓。更改背景或字体颜色,哪个都无所谓)。

我最初是在 ListBoxes 并排显示这些,但是当我发现很难以编程方式更改特定单元格的颜色时,我开始查看 DataGrid,但这也被证明是困难的 + 我发现的许多指南online 似乎是专注于 Windows 窗体,而不是 WPF。

对于我认为是一项简单的任务,推荐的控件是什么,请指导我如何识别这些不同的单元格/索引并更新它们的外观。

额外信息:两个列表都不会太长(每个

我在使用数据网格的时候试过这个;

dgdResults.Rows[0].Cells[0].Style.BackColor = Color.Red;

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    DataGrid/ListBox 是 ItemsControls,可以很好地与单个集合一起使用。我建议使用 list1 和 list2 and 属性中的一对项目创建额外的对象列表,该属性指示不同的项目,然后在 UI 中显示该列表。

    var L1 = new List<int> {1, 2, 3, 4, 5};
    var L2 = new List<int> {1, 2, 0, 4, 5};
    var results = L1.Zip(L2, (i, j) => new {Previous = i, Current = j, IsDifferent = i != j});
    ListResults.ItemsSource = results;
    
    <ItemsControl Name="ListResults">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <UniformGrid Rows="1">
                    <UniformGrid.Style>
                        <Style TargetType="UniformGrid">
                            <Style.Triggers>
                              <DataTrigger Binding="{Binding Path=IsDifferent}" Value="true">
                                <Setter Property="Background" Value="Crimson"/>
                              </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </UniformGrid.Style>
                    <Label Content="{Binding Path=Previous}"/>
                    <Label Content="{Binding Path=Current}"/>
                </UniformGrid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    


    DataTable/DataGrid 的替代方案

    var L1 = new List<int> {1, 2, 3, 4, 5};
    var L2 = new List<int> {1, 2, 0, 4, 5};
    
    var dt = new DataTable
                {
                    Columns =
                    {
                        {"Previous", typeof (int)},
                        {"Current", typeof (int)},
                        {"IsDifferent", typeof (bool)},
                    }
                };
    for (int x = 0; x < L1.Count; x++)
        dt.Rows.Add(L1[x], L2[x], L1[x] != L2[x]);
    
    DgResults.ItemsSource = dt.DefaultView;
    
    <DataGrid Name="DgResults" AutoGenerateColumns="False" ColumnWidth="*" IsReadOnly="True">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsDifferent}" Value="true">
                        <Setter Property="Background" Value="Crimson"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Previous}"/>
            <DataGridTextColumn Binding="{Binding Path=Current}"/>
        </DataGrid.Columns>
    </DataGrid>
    

    属性IsDifferent在DataTrigger中用于改变背景

    【讨论】:

    • 真棒又直接!有没有办法修改ItemSource 行以使用带有Previous、Current、IsDifferent 列的DataTable?
    • @windowskm,是的:ListResults.ItemsSource = myDataTable.DefaultView;
    【解决方案2】:

    在您的 app.xaml 中创建一个您想要用于不匹配的行的样式。

    <Style TargetType="DataGridRow">
         <Setter Property="Background" Value="Red"/>
    </Style>
    

    如果您随后枚举行并找到不匹配的行,则可以像这样设置样式

    Style style = Application.Current.FindResource("DataGridRow") as Style;
    row.Style = style;
    

    【讨论】:

      【解决方案3】:

      从这里How to change single cell color of datagrid in wpf? 和 user1064519 的回答我明白了

      DataGridRow firstRow = dataGrid1.ItemContainerGenerator.ContainerFromItem(dataGrid1.Items[0]) as DataGridRow;
      DataGridCell firstColumnInFirstRow = dataGrid1.Columns[0].GetCellContent(firstRow).Parent as DataGridCell;
      firstColumnInFirstRow.Background = Brushes.Red;
      

      这对我有用!明显改变其他行/列的索引

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-06
        • 2021-03-31
        • 1970-01-01
        • 1970-01-01
        • 2014-11-29
        • 1970-01-01
        • 2010-12-21
        • 2012-02-19
        相关资源
        最近更新 更多