【发布时间】:2016-08-21 05:42:05
【问题描述】:
我正在尝试使用 ItemContainerStyleSelector 根据定义行的对象的类型在数据网格中显示不同的行样式(ItemsSource 是IGridItems 的集合,有GridItem 和GridSeparator这应该得到不同的风格)。我的问题是,我的 StyleSelector 的 SelectStyle 从未被调用过。现在我发现 (here) 问题可能是继承样式(MahApps Metro 库重新定义了所有标准控件的默认样式),它可能已经设置了 ItemContainerStyle。
所以现在我的问题是:有没有办法仍然使用我的 StyleSelector,以便我将继承的样式作为所选样式的基础?如果没有,我如何根据对象类型为某些行实现不同的样式?
编辑:
手动将 ItemContainerStyle 设置为 null 没有效果,我的 StyleSelector 的 SelectStyle 仍然没有被调用。
EDIT2:
因为我没有像 Grx70 那样得到System.Windows.Data Error: 24 : Both 'ItemContainerStyle' and 'ItemContainerStyleSelector' are set; 'ItemContainerStyleSelector' will be ignored.,所以我假设 ItemContainerStyle 不是问题,就像我最初想的那样。
jstreet 指出,它与 MahApps.Metro 相关,不过......(见他的评论)
我目前的实现:
<DataGrid ItemsSource="{Binding Items}" ItemContainerStyleSelector="{StaticResource StyleSelector}">
样式选择器:
public class GridRowStyleSelector : StyleSelector
{
private readonly ResourceDictionary _dictionary;
public GridRowStyleSelector()
{
_dictionary = new ResourceDictionary
{
Source = new Uri(@"pack://application:,,,/myApp;component/View/GridResources.xaml")
};
}
public override Style SelectStyle(object item, DependencyObject container)
{
string name = item?.GetType().Name;
if (name != null && _dictionary.Contains(name))
{
return (Style)_dictionary[name];
}
return null;
}
}
带有测试值的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>
【问题讨论】:
-
只是把它从桌面上拿下来 - 你确定
{StaticResource StyleSelector}解析为GridRowStyleSelector实例吗? -
Select 样式的方法签名是 public override Style SelectStyle(object item, DependencyObject container)
-
此外,您是否在输出窗口中收到
System.Windows.Data Error: 24 : Both 'ItemContainerStyle' and 'ItemContainerStyleSelector' are set; 'ItemContainerStyleSelector' will be ignored.错误? -
@Grx70 是的,我知道实例已解决,因为我可以中断构造函数,不,我没有在任何地方看到该错误
-
确实,
StyleSelector.SelectStyle()方法在常规 WPF 应用程序中被调用,但在使用MahApps.Metro时不会。看起来它被完全忽略了。顺便说一句,在任何一种情况下都没有例外。
标签: c# wpf mvvm mahapps.metro