【问题标题】:Set datagrid row color based on the value in the datagrid根据数据网格中的值设置数据网格行颜色
【发布时间】:2012-09-18 12:50:46
【问题描述】:

Datagrid dgColor 包含两个字段:

  • columnA 显示 a、b、c 等名称。
  • columnB 是一个隐藏字段,其中包含(#ffffff, #ff1211, #1111, #1222) 等颜色代码值。

根据columnB中的值填充数据网格行颜色。

【问题讨论】:

  • 你已经尝试了什么,你卡在哪里了?

标签: wpf wpf-controls wpfdatagrid


【解决方案1】:

试一试,但我没有用数据测试它

    <DataGrid Name="dataGrid1" Margin="12,12,0,0">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="Background" Value="{Binding Path=colorCol}" />
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>

【讨论】:

  • not Working 显示错误消息,例如“输入字符串的格式不正确。” . "Red" 和 '#ff00ffff' 值用于测试
  • 至少它得到了一个价值。使用转换器将字符串转换为画笔。
【解决方案2】:

由于您已经获得了每行所需的颜色,因此您可以在 LoadingRow 事件中轻松设置每行的颜色。我无法测试我现在所在的代码,因此您可能需要对其进行一些调整,但您可以尝试以下操作:

private void dataGrid_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
{
    MyObject myObject = e.Row.Item as MyObject;
    if (myObject != null)
    {
        byte r = byte.Parse(myObject.Color.Substring(1, 2), NumberStyles.HexNumber);
        byte g = byte.Parse(myObject.Color.Substring(3, 2), NumberStyles.HexNumber);
        byte b = byte.Parse(myObject.Color.Substring(5, 2), NumberStyles.HexNumber);

        e.Row.Background = new SolidColorBrush(Color.FromRgb(r,g,b));
    }

}

编辑: 请改用编辑后的代码。它应该可以完成这项工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多