C# 设置数据网格的样式

C# 设置数据网格的样式,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,一旦设置了数据绑定,如何根据列中设置的值设置datagrid的每一行的样式?我会使用代码隐藏还是使用XAML风格的触发器来设计它 <DataGrid x:Name="bla"> // use this column if you want to style the header too <DataGrid.RowHeaderStyle> <Style TargetType="DataGridRowHeader">

一旦设置了数据绑定,如何根据列中设置的值设置datagrid的每一行的样式?我会使用代码隐藏还是使用XAML风格的触发器来设计它

<DataGrid x:Name="bla">
    // use this column if you want to style the header too
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
               AncestorType={x:Type DataGridRow}},
               Converter={StaticResource ExcelRowColorConverter}}">
            </Setter>
            <Setter Property="BorderThickness" Value="1,0,1,1"></Setter>
            <Setter Property="BorderBrush" Value="#FF000000"></Setter>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="DataGrid.Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" >
                        <MultiBinding.Bindings>
我的问题是,它将取决于列[0]中设置的值

<DataGrid x:Name="bla">
    // use this column if you want to style the header too
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
               AncestorType={x:Type DataGridRow}},
               Converter={StaticResource ExcelRowColorConverter}}">
            </Setter>
            <Setter Property="BorderThickness" Value="1,0,1,1"></Setter>
            <Setter Property="BorderBrush" Value="#FF000000"></Setter>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="DataGrid.Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" >
                        <MultiBinding.Bindings>
假设列[0]的值为1,2,3。我想根据这个值设置datagrid行的样式

<DataGrid x:Name="bla">
    // use this column if you want to style the header too
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
               AncestorType={x:Type DataGridRow}},
               Converter={StaticResource ExcelRowColorConverter}}">
            </Setter>
            <Setter Property="BorderThickness" Value="1,0,1,1"></Setter>
            <Setter Property="BorderBrush" Value="#FF000000"></Setter>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="DataGrid.Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" >
                        <MultiBinding.Bindings>
范例

    c#
    DataGrid1.ItemsSource = schDT.DefaultView

    xaml
    <DataGridTextColumn Binding="{Binding ITEMNUMBER}" Header="ITEMNUMBER" />
    <DataGridTextColumn Binding="{Binding CODE}" Header="CODE" />
    <DataGridTextColumn Binding="{Binding DESC}" Header="STD DESCRIPTION" />
<DataGrid x:Name="bla">
    // use this column if you want to style the header too
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
               AncestorType={x:Type DataGridRow}},
               Converter={StaticResource ExcelRowColorConverter}}">
            </Setter>
            <Setter Property="BorderThickness" Value="1,0,1,1"></Setter>
            <Setter Property="BorderBrush" Value="#FF000000"></Setter>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="DataGrid.Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" >
                        <MultiBinding.Bindings>
c#
DataGrid1.ItemsSource=schDT.DefaultView
xaml

感谢您一如既往的帮助这是一个相当棘手的问题,我不久前就不得不解决。
<DataGrid x:Name="bla">
    // use this column if you want to style the header too
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
               AncestorType={x:Type DataGridRow}},
               Converter={StaticResource ExcelRowColorConverter}}">
            </Setter>
            <Setter Property="BorderThickness" Value="1,0,1,1"></Setter>
            <Setter Property="BorderBrush" Value="#FF000000"></Setter>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="DataGrid.Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" >
                        <MultiBinding.Bindings>
我使用了以下代码:

<DataGrid x:Name="bla">
    // use this column if you want to style the header too
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
               AncestorType={x:Type DataGridRow}},
               Converter={StaticResource ExcelRowColorConverter}}">
            </Setter>
            <Setter Property="BorderThickness" Value="1,0,1,1"></Setter>
            <Setter Property="BorderBrush" Value="#FF000000"></Setter>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="DataGrid.Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" >
                        <MultiBinding.Bindings>
基本上:您需要一个Valueconverter来模拟获取行/单元格的值。 在这里的示例中,我使用了一个多值转换器,因为我也需要有关Colzumn的信息-根据场景的不同,一个普通值转换器就足够了

<DataGrid x:Name="bla">
    // use this column if you want to style the header too
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
               AncestorType={x:Type DataGridRow}},
               Converter={StaticResource ExcelRowColorConverter}}">
            </Setter>
            <Setter Property="BorderThickness" Value="1,0,1,1"></Setter>
            <Setter Property="BorderBrush" Value="#FF000000"></Setter>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="DataGrid.Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" >
                        <MultiBinding.Bindings>
XAML //相反

<DataGrid x:Name="bla">
    // use this column if you want to style the header too
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
               AncestorType={x:Type DataGridRow}},
               Converter={StaticResource ExcelRowColorConverter}}">
            </Setter>
            <Setter Property="BorderThickness" Value="1,0,1,1"></Setter>
            <Setter Property="BorderBrush" Value="#FF000000"></Setter>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="DataGrid.Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" >
                        <MultiBinding.Bindings>

//如果也要设置标题样式,请使用此列
//这是设计特定单元格样式的关键部分

<DataGrid x:Name="bla">
    // use this column if you want to style the header too
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
               AncestorType={x:Type DataGridRow}},
               Converter={StaticResource ExcelRowColorConverter}}">
            </Setter>
            <Setter Property="BorderThickness" Value="1,0,1,1"></Setter>
            <Setter Property="BorderBrush" Value="#FF000000"></Setter>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="DataGrid.Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" >
                        <MultiBinding.Bindings>
C#码

<DataGrid x:Name="bla">
    // use this column if you want to style the header too
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
               AncestorType={x:Type DataGridRow}},
               Converter={StaticResource ExcelRowColorConverter}}">
            </Setter>
            <Setter Property="BorderThickness" Value="1,0,1,1"></Setter>
            <Setter Property="BorderBrush" Value="#FF000000"></Setter>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="DataGrid.Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" >
                        <MultiBinding.Bindings>
//-----------------------------------------------------------------------
// 
//版权所有(c)最佳研究。版权所有。
// 
//-----------------------------------------------------------------------
命名空间myTool.Utils
{
使用制度;
使用System.Collections.Generic;
使用系统数据;
利用制度全球化;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Media;
/// 
///用于将单元格转换为其最终颜色。它根据行和列来确定。
/// 
公共类ExcelDataGridCellColorConverter:IMultiValueConverter
{
/// 
///用于将单元格转换为其最终颜色。它根据行和列来确定。
/// 
///参数数组
///目标类型。
///参数。
///文化。
/// 
///刷子
/// 
公共对象转换(对象[]值,类型targetType,对象参数,CultureInfo区域性)
{
如果(值[1]为数据行)
{
变量单元格=(DataGridCell)值[0];
变量行=(数据行)值[1];
var columnName=cell.Column.SortMemberPath;
var table=row.table;
//之所以使用此选项,是因为WPF中存在一个错误,导致“看到”已删除的数据行。删除空行时会发生这种情况。应通过删除方法添加键“锁”,然后再删除。
if(table.ExtendedProperties.Contains(“锁”))
{
盖恩斯博罗;
}
var colValue=table.Rows[table.Rows.Count-1][columnName];
var rowValue=row[Properties.Resources.ColorColumnName];
var colorColValue=(Enums.RowState)Enum.Parse(typeof(Enums.RowState),colValue.ToString(),true);
var colorRowValue=(Enums.RowState)Enum.Parse(typeof(Enums.RowState),rowValue.ToString(),true);
if(colorColValue==Enums.RowState.isinclude&&colorRowValue==Enums.RowState.isinclude)
{
返回笔刷。浅绿色;
}
else if(colorRowValue==Enums.RowState.HeaderRow)
{
盖恩斯博罗;
}
其他的
{ 
返回刷。轻鲑鱼;
}
}
返回笔刷。蓝色;
}
/// 
///不用
/// 
///参数
///目标类型。
///参数。
///文化。
/// 
///刷子
/// 
公共对象[]转换回(对象值,类型[]目标类型,对象参数,CultureInfo区域性)
{
抛出新系统。NotImplementedException();
}
}
}

找到答案,抱歉,在发布问题之前必须仔细查看

<DataGrid x:Name="bla">
    // use this column if you want to style the header too
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
               AncestorType={x:Type DataGridRow}},
               Converter={StaticResource ExcelRowColorConverter}}">
            </Setter>
            <Setter Property="BorderThickness" Value="1,0,1,1"></Setter>
            <Setter Property="BorderBrush" Value="#FF000000"></Setter>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="DataGrid.Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" >
                        <MultiBinding.Bindings>

谢谢Christian,这是我的另一个选择,我找到了我想要的答案(见上文)。感谢您抽出时间回答我的问题。请关闭问题。这里没有复制品。
<DataGrid x:Name="bla">
    // use this column if you want to style the header too
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
               AncestorType={x:Type DataGridRow}},
               Converter={StaticResource ExcelRowColorConverter}}">
            </Setter>
            <Setter Property="BorderThickness" Value="1,0,1,1"></Setter>
            <Setter Property="BorderBrush" Value="#FF000000"></Setter>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="DataGrid.Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" >
                        <MultiBinding.Bindings>