在WPF Datagrid中的样式第一行 [英] Style first row in a WPF Datagrid
问题描述
我想在WPF Datagrid中更改第一行(仅)的样式,但尚未找到如何执行此操作。我想知道如何创建触发器:
< Style TargetType ={x:Type dg:DataGridRow} >
< Style.Triggers>
< Trigger Property =SelectedIndexValue =0>
< Setter Property =BackgroundValue =Red/>
< / Trigger>
< /Style.Triggers>
< / Style>
但是当然这不起作用,因为DataGridRow上没有SelectedIndex属性。我也有一些尝试在我的代码中执行此操作,但无法使其正常工作。
看起来似乎相当简单,但我没有管理它,所以任何建议将是非常感谢。
谢谢,
将
您可能可以创建一个IValueConverter来返回您的样式,作为一个Style对象,或者只是一个字符串表示(即风格名称)。然后,您可以将DataGrid的style属性绑定到转换器,并将项目的基础列表作为参数传递,以确定当前项目的索引?
转换器可能看起来像这样...
public class StyleConverter:IValueConverter
{
public object Convert对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化)
{
Style style1 = App.Current.FindResource(RowStyle1)as Style;
Style style2 = App.Current.FindResource(RowStyle2)as Style;
列表< object> items =参数作为List< object> ;;;
if(items [0] == value)
{
return style1;
}
return style2;
}
}
不知道这是否会起作用,我很好奇,现在,我很好奇,可以试试看看能否让它上班!
I would like to change the styling of the first row (only) in a WPF Datagrid but haven't found how to do it. 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. 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). 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?
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!
这篇关于在WPF Datagrid中的样式第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!