如何更改行的风格在WPF Datagrid的? [英] how to change a row's style in WPF Datagrid?

查看:280
本文介绍了如何更改行的风格在WPF Datagrid的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来WPF,并且需要在运行时的造型一个特定的行或列在DataGrid,取决于存储的数据的一些值帮助。
我看到这个线程但couldn T retrive我行的问题,只存在于从存在于设计
列运行。

I am new to WPF, and need help in styling a specific row or rows in a datagrid in runtime, depending on some values of the stored data. I saw this thread but couldn't retrive my problem of row that exist only in runtime from columns that exist in design.

任何例子将欣然接受。
感谢

any example will be gladly accepted. thanks!

编辑:我发现这个的过,但我需要一个C#代码...

edited: I found this thread too, but I need a C# code...

推荐答案

是否要添加列网格在运行时我无法从你的问题告诉我们,但无论哪种方式,你可以在设计时使用DataTriggers处理您的具体造型需求CellStyle添加到网格中。

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.

例如,下面的就可以把所有的行红所在的名称属性=比利鲍伯松:

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.

该转换器应该是这样的:

The Converter would look like this:

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的控制的文本属性来。您可以更改绑定到任何你想要的。

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.

这篇关于如何更改行的风格在WPF Datagrid的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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