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

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

问题描述

在运行时如何为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天全站免登陆