简体   繁体   English

在WPF Datagrid中设置第一行样式

[英]Style first row in a WPF Datagrid

I would like to change the styling of the first row (only) in a WPF Datagrid but haven't found how to do it. 我想改变WPF Datagrid中第一行(仅)的样式,但还没有找到如何做到这一点。 I wondered about creating a trigger, something like this: 我想知道创建一个触发器,如下所示:

<Style TargetType="{x:Type dg:DataGridRow}">
    <Style.Triggers>
        <Trigger Property="SelectedIndex" Value="0">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>

But of course this doesn't work since there is no 'SelectedIndex' property on DataGridRow. 但是当然这不起作用,因为DataGridRow上没有'SelectedIndex'属性。 I have also had some attempts at doing this in my code behind but couldn't get it to work. 我在我的代码背后也尝试过这样做,但无法让它工作。

It seems like something that out to be fairly simple but I haven't managed it, so any advice would be most appreciated. 看起来似乎很简单,但我没有管理它,所以任何建议都会受到最高的赞赏。

Thanks, Will 谢谢,威尔

You might be able to create an IValueConverter to return your Style, either as a Style object or just a string representation (ie. the name of the style). 您可以创建一个IValueConverter来返回Style,可以是Style对象,也可以只是字符串表示(即样式的名称)。 Then you can bind the style property of your DataGrid to the converter and pass in the underlying list of items as a parameter to determine the index of the current item? 然后,您可以将DataGrid的样式属性绑定到转换器,并将基础项目列表作为参数传递,以确定当前项目的索引?

The converter might look something like this... 转换器可能看起来像这样......

public class StyleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Style style1 = App.Current.FindResource("RowStyle1") as Style;
        Style style2 = App.Current.FindResource("RowStyle2") as Style;

        List<object> items = parameter as List<object>;

        if (items[0] == value)
        {
            return style1;
        }

        return style2;
    }
}

Not sure if this would work, I probably haven't explained it very well either! 不确定这是否可行,我可能也没有解释得很好!

I'm curious now, I might give this a try and see if I can get it to work! 我现在很好奇,我可以尝试一下,看看我是否可以让它发挥作用!

I don't know of a way to do this, but it is possible to freeze a row. 我不知道如何做到这一点,但可以冻结一行。 Does that suit your needs? 这符合您的需求吗? The code in the following link might lead you to a solution on how to get access to a specific row so that you can apply a style to it. 以下链接中的代码可能会引导您获得有关如何访问特定行的解决方案,以便您可以对其应用样式。

http://blogs.msdn.com/vinsibal/archive/2008/10/31/wpf-datagrid-frozen-row-sample.aspx http://blogs.msdn.com/vinsibal/archive/2008/10/31/wpf-datagrid-frozen-row-sample.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM