C# 行的WPF DataGrid样式选择器

C# 行的WPF DataGrid样式选择器,c#,wpf,mvvm,mahapps.metro,C#,Wpf,Mvvm,Mahapps.metro,我试图使用ItemContainerStyleSelector根据定义行的对象的类型,在datagrid中显示不同的行样式(ItemsSource是IGridItems的集合,有GridItem和GridSeparator,它们应该具有不同的样式)。我的问题是,我的样式选择器的SelectStyle从未被调用。现在我发现()问题可能是继承的样式(MahApps Metro库重新定义了所有标准控件的默认样式),它可能已经设置了ItemContainerStyle 所以现在我的问题是:有没有办法继续

我试图使用ItemContainerStyleSelector根据定义行的对象的类型,在datagrid中显示不同的行样式(ItemsSource是
IGridItem
s的集合,有
GridItem
GridSeparator
,它们应该具有不同的样式)。我的问题是,我的样式选择器的
SelectStyle
从未被调用。现在我发现()问题可能是继承的样式(MahApps Metro库重新定义了所有标准控件的默认样式),它可能已经设置了ItemContainerStyle

所以现在我的问题是:有没有办法继续使用我的样式选择器,这样我就可以将继承的样式作为所选样式的基础?如果没有,如何根据某些行的对象类型实现不同的样式

编辑:
手动将ItemContainerStyle设置为
null
没有效果,我的样式选择器的
SelectStyle
仍然不会被调用

编辑2:
因为我没有获得
System.Windows.Data错误:24:设置了'ItemContainerStyle'和'ItemContainerStyleSelector'ItemContainerStyleSelector'将被忽略。
就像Grx70所问的那样,我假设ItemContainerStyle不是问题所在,正如我最初所想的那样

jstreet指出,它与MahApps.Metro有关,不过。。。(见他的评论)


我目前的执行情况:

<DataGrid ItemsSource="{Binding Items}" ItemContainerStyleSelector="{StaticResource StyleSelector}">
GridResources.xaml和测试值:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="GridItem" TargetType="DataGridRow">
        <Setter Property="BorderThickness" Value="3"/>
    </Style>
    <Style x:Key="GridSeparator"  TargetType="DataGridRow">
        <Setter Property="BorderBrush" Value="Red"/>
    </Style>
</ResourceDictionary>

您可以通过显式地将默认的
ItemContainerStyle
设置为
null
来覆盖它:

<DataGrid ItemsSource="{Binding Items}"
          ItemContainerStyle="{x:Null}"
          ItemContainerStyleSelector="{StaticResource StyleSelector}">

您可以通过显式地将默认的
ItemContainerStyle
设置为
null
来覆盖它:

<DataGrid ItemsSource="{Binding Items}"
          ItemContainerStyle="{x:Null}"
          ItemContainerStyleSelector="{StaticResource StyleSelector}">

GridRowStyleSelector类的构造函数是静态和私有的。
尝试从此类中删除“static”关键字,并将构造函数公开。

GridRowStyleSelector类的构造函数是静态和私有的。
尝试从这个类中删除“static”关键字,并公开构造函数。

我想我已经找到了罪魁祸首。事实证明,在
DataGrid
中处理行样式的正确方法是通过
RowStyle
RowStyleSelector
属性,而不是
ItemContainerStyle
ItemContainerStyleSelector
。它们可以工作,但只有在明确使用
RowStyle
RowStyleSelector
之前才能工作。这正是MahApps Metro所做的-它设置
RowStyle
值(我相信通过覆盖
DataGrid
default样式)。然后我认为,
ItemContainerStyle
是由
DataGrid
在内部设置的(一些测试显示,
ItemContainerStyle
是设置的,尽管它显式地设置为
null

总之,这对你来说应该是个好办法:

<DataGrid ItemsSource="{Binding Items}"
          RowStyle="{x:Null}"
          RowStyleSelector="{StaticResource StyleSelector}">
    (...)
</DataGrid>

(...)
此外,要修改MahApps行样式而不是完全放弃它,您应该基于MahApps行样式:

<Style x:Key="GridItem" TargetType="DataGridRow"
       BasedOn="{StaticResource MetroDataGridRow}">
    <Setter Property="BorderThickness" Value="3"/>
</Style>
<Style x:Key="GridSeparator" TargetType="DataGridRow"
       BasedOn="{StaticResource MetroDataGridRow}">
    <Setter Property="BorderBrush" Value="Red"/>
</Style>

我想我找到了罪犯。事实证明,在
DataGrid
中处理行样式的正确方法是通过
RowStyle
RowStyleSelector
属性,而不是
ItemContainerStyle
ItemContainerStyleSelector
。它们可以工作,但只有在明确使用
RowStyle
RowStyleSelector
之前才能工作。这正是MahApps Metro所做的-它设置
RowStyle
值(我相信通过覆盖
DataGrid
default样式)。然后我认为,
ItemContainerStyle
是由
DataGrid
在内部设置的(一些测试显示,
ItemContainerStyle
是设置的,尽管它显式地设置为
null

总之,这对你来说应该是个好办法:

<DataGrid ItemsSource="{Binding Items}"
          RowStyle="{x:Null}"
          RowStyleSelector="{StaticResource StyleSelector}">
    (...)
</DataGrid>

(...)
此外,要修改MahApps行样式而不是完全放弃它,您应该基于MahApps行样式:

<Style x:Key="GridItem" TargetType="DataGridRow"
       BasedOn="{StaticResource MetroDataGridRow}">
    <Setter Property="BorderThickness" Value="3"/>
</Style>
<Style x:Key="GridSeparator" TargetType="DataGridRow"
       BasedOn="{StaticResource MetroDataGridRow}">
    <Setter Property="BorderBrush" Value="Red"/>
</Style>


好的,我试过了,但我仍然没有停止在
SelectStyle
。。。似乎我的样式选择器还有另一个被忽略的原因:/ok,我尝试了这个,但我仍然没有停止在
SelectStyle
。。。似乎我的样式选择器还有另一个被忽略的原因:/I我仍然没有输入
SelectStyle
。。。旧的构造函数和非静态构造函数一样工作,如果我设置了断点,它将停止在那里,我仍然不会进入
SelectStyle
。。。旧构造函数和非静态构造函数一样工作,如果我设置断点,它会停在那里,只是为了将其从表中删除-您确定
{StaticResource StyleSelector}
解析为
GridRowStyleSelector
实例吗?选择样式的方法签名是public override style SelectStyle(对象项,从属对象容器)此外,您是否收到
System.Windows.Data错误:24:设置了'ItemContainerStyle'和'ItemContainerStyleSelector','ItemContainerStyleSelector'将被忽略。
输出窗口中的错误?@Grx70是的,我知道该实例已解决,因为我可以在构造函数中中断,不,我在任何地方都没有看到该错误ed,
StyleSelector.SelectStyle()
方法在常规WPF应用程序中被调用,但在使用
MahApps.Metro
时被调用。看起来它被完全忽略了。顺便说一句,在这两种情况下都没有例外。只是为了将其从表中删除-您确定
{StaticResource StyleSelector}
解析为
GridRowStyleSelector
实例?选择样式的方法签名是公共覆盖样式SelectStyle(对象项,DependencyObject容器)此外,您是否收到
System.Windows.Data错误:24:设置了'ItemContainerStyle'和'ItemContainerStyleSelector','ItemContainerStyleSelector'将被忽略。
输出窗口中的错误?@Grx70 yea我知道实例已解决,si