Friday, April 15, 2011

Why won't this change the color of my WPF Datagrid headers?

I fill my datagrid in code behind like this:

var customers = from c in _db.Customers
                select c;
TheDataGrid.ItemsSource = customers.ToList();

In my XAML below, the DataGrid.RowBackground works but the DataGridHeaderBorder gets the error "The Items listing must be empty before the use of ItemsSource" in the code-behind upon runtime.

This is odd because I'm just trying to change the color of the Datagrid headers and it gets a problem with ItemsSource. If I take the DataGridHeaderBorder line out, it works fine.

So how does one simply change the color of the column headers on a Datagrid?

        <toolkit:DataGridHeaderBorder Background="yellow"/>

        <toolkit:DataGrid.RowBackground>
            <LinearGradientBrush EndPoint="-0.136,-0.163" StartPoint="1.291,1.248">
                <GradientStop Color="#FFA8A929" Offset="0.004"/>
                <GradientStop Color="#FFF7F7ED" Offset="0.991"/>
            </LinearGradientBrush>
        </toolkit:DataGrid.RowBackground>
From stackoverflow
  • Any time that you put elements within elements, the XAML parser has to decide if the sub-elements are complex property assignments (as you are trying to do), or whether you intend the sub-elements to be the "content" of the parent element. Because your problem "DataGridHeaderBorder" tag does not start with "toolkit:DataGrid", it is assumed that you are trying to set the DataGrid's content property (which happens to be Items) to this value.

    I am not in a position to try this, but I would guess you'd need to replace the problem tag with something like:

    <toolkit:DataGrid.RowHeaderStyle>
        <Style TargetType="{x:Type toolkit:DataGridRowHeader}">
            <Setter Property="Background" Value="Yellow" />
        </Style>
    </toolkit:DataGrid.RowHeaderStyle>
    

0 comments:

Post a Comment