如何根据行数据动态改变行的样式? [英] How To Dynamically Change a Row's Style Based On Row Data?

查看:13
本文介绍了如何根据行数据动态改变行的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时为 WPF Datagrid 中的特定行设置样式?每次更改都取决于显示数据的某些值?

How does one style a specific row or rows in a WPF Datagrid during runtime? Each change depends on some values of the shown data?

推荐答案

我无法从您的问题中判断您是否在运行时向网格添加列,但无论哪种方式,您都可以在设计时向网格添加 CellStyle使用 DataTriggers 处理您的特定样式需求的时间.

I can't tell from your question whether you are adding columns to your grid at run time, but either way you can add a CellStyle to the grid at design time that handles your specific styling needs using DataTriggers.

例如,以下内容将使 Name 属性 = "Billy Bob" 的所有行变为红色:

For instance, the following would make all rows red where the Name property = "Billy Bob":

    <DataGrid AutoGenerateColumns="True" Name="dataGrid1">
        <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Name}" Value="Billy Bob" >
                        <Setter Property="Background" Value="Red" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>

如果您在运行时以编程方式添加列,并且想要对它们应用某种样式,您仍然可以在设计时在您的 xaml 中定义这些样式.

If you are adding columns programmatically at run time and you want to apply a certain style to them, you can still define those styles at design time in your xaml.

    <DataGrid AutoGenerateColumns="False" Name="dataGrid1">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridCell}" x:Key="MyCellStyle">
                <Setter Property="Foreground" Value="Green"/>
            </Style>
        </DataGrid.Resources>
        ...

然后,当您添加列时,您可以将该样式应用于它们:

Then when you are adding the columns you can apply that style to them:

col.CellStyle = (Style)dataGrid1.Resources("MyCellStyle");

更新

如果你有一个歌曲列表,并且你想改变每首歌曲的行颜色,其中的艺术家的名字以a"开头,那么你可以使用 IValueConverter.

If you have a list of songs and you want to change the row color of every song that has an artist whose name starts with an "a", then you could use an IValueConverter.

以下转换器可以解决问题:

The following converter would do the trick:

public class ArtistNameConverter : IValueConverter
{
    public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        try
        {
            return value.ToString().StartsWith(parameter.ToString());
        }
        catch
        {
            return false;
        }
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

然后你可以像这样在你的 xaml 中使用转换器:

Then you could use the converter in your xaml like so:

    <DataGrid AutoGenerateColumns="True" Name="dataGrid1">
        <DataGrid.Resources>
            <converters:ArtistNameConverter x:Key="ArtistNameConverter"></converters:ArtistNameConverter>
        </DataGrid.Resources>
        <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ArtistName, Converter={StaticResource ArtistNameConverter}, ConverterParameter=a}" Value="True" >
                        <Setter Property="Background" Value="Red" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>

注意我是如何将a"作为参数传递给转换器的.您可以传入任何您想要的字母,并且以该字母开头的艺术家的行将其背景颜色设置为红色.

Notice how I am passing an "a" into the converter as the parameter. You can pass in whatever letter you want, and the rows that have artists that start with that letter will have their background color set to red.

更新 2

如果您想将某种变量传递给转换器,您可以使用 MultiBinding.

If you want to pass in a variable of some sort to the converter, you can use MultiBinding.

转换器看起来像这样:

public class ArtistNameConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, 
        System.Globalization.CultureInfo culture)
    {
        try
        {
            return values[0].ToString().StartsWith(values[1].ToString());
        }
        catch
        {
            return false;
        }
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, 
        System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

传入的第一个参数是艺术家姓名,第二个是字母.

The first parameter passed in is the artist name, the second is the letter.

你会像这样在你的网格中使用它:

And you would use it in your grid like this:

        <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                    <DataTrigger Value="True" >
                        <DataTrigger.Binding>
                            <MultiBinding Converter="{StaticResource ArtistNameConverter}">
                                <Binding Path="ArtistName" />
                                <Binding Mode="OneWay" ElementName="FirstLetter" Path="Text" />
                            </MultiBinding>
                        </DataTrigger.Binding>
                        <Setter Property="Background" Value="Red" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>

在本例中,第一个字母来自名为FirstLetter"的控件的Text"属性.您可以将绑定更改为您想要的任何内容.

In this example, the first letter is coming from the "Text" property of a control called "FirstLetter". You can change that binding to whatever you want.

这篇关于如何根据行数据动态改变行的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆