问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/9623820/rowstyleselector-not-called