views:

434

answers:

2

Hi,

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

A: 

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

viggity
+1  A: 

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!

TabbyCool