Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RowStyleSelector Not Called

I'm having difficulty getting a RowStyleSelector to work with a WPF DataGrid.

In my resources, I have

<loc:DetailsRowStyleSelector x:Key="detailsRowStyleSelector" AddRowStyle="{StaticResource newItemRowStyle}" StandardRowStyle="{StaticResource RowStyle}"/>

Then my datagrid uses this like so:

<DataGrid ...
    EnableRowVirtualization="false"      
    VirtualizingStackPanel.VirtualizationMode="Standard"
    RowStyleSelector="{StaticResource detailsRowStyleSelector}"

The constructor for the selector is called, but the SelectStyle method is not, and my rows all look the same. There seems to be very little documentation on this, but this is what my selector looks like:

public class DetailsRowStyleSelector : StyleSelector {
    public Style AddRowStyle { get; set; }
    public Style StandardRowStyle { get; set; }

    public DetailsRowStyleSelector() {
        Console.WriteLine(""); // this is called
    }

    public override Style SelectStyle(object item, DependencyObject container) {
        // this is not called
like image 554
Echilon Avatar asked Mar 08 '12 19:03

Echilon


1 Answers

The most likely cause is that you have either the RowStyle or ItemContainerStyle set on the DataGrid, either locally or through an inherited style.

The RowStyleSelector overrides the ItemContainerStyleSelector, which includes the following statement in the documentation:

Note that this property is ignored if the ItemContainerStyle property is set.

like image 143
Richard Deeming Avatar answered Sep 28 '22 04:09

Richard Deeming