RowStyleSelector Not Called

前提是你 提交于 2019-12-10 15:30:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!