- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用ItemContainerStyleSelector根据定义行的对象的类型在数据网格中显示不同的行样式(ItemsSource是IGridItem
的集合,有GridItem
和GridSeparator
应该具有不同的样式)。我的问题是,从未调用过我的StyleSelector的SelectStyle
。现在,我发现(here)问题可能是继承的样式(MahApps Metro库重新定义了所有标准控件的默认样式),这可能已经设置了ItemContainerStyle。
所以现在我的问题是:是否有办法仍然使用我的StyleSelector,以便将继承的样式作为所选样式的基础?如果不是,如何根据对象类型为某些行实现不同的样式?
编辑:
手动将ItemContainerStyle设置为null
无效,仍然不会调用我的StyleSelector的SelectStyle
。
编辑2:
由于我没有像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;
}
}
<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>
最佳答案
我想我找到了罪魁祸首。事实证明,处理DataGrid
中的行样式的正确方法是通过RowStyle
和RowStyleSelector
属性,而不是ItemContainerStyle
和ItemContainerStyleSelector
。它们有效,但仅在您显式使用RowStyle
或RowStyleSelector
之前有效。这正是MahApps Metro所做的-它设置RowStyle
值(我相信通过覆盖DataGrid
默认样式)。然后我认为ItemContainerStyle
是由DataGrid
在内部设置的(一些测试表明,尽管ItemContainerStyle
明确设置为null
,但仍已设置)。
综上所述,这应该为您解决问题:
<DataGrid ItemsSource="{Binding Items}"
RowStyle="{x:Null}"
RowStyleSelector="{StaticResource StyleSelector}">
(...)
</DataGrid>
<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>
关于c# - 行的WPF DataGrid StyleSelector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36881121/
我需要根据数据中的值更改按钮的样式。看起来 StyleSelector 可以完美地工作,但似乎没有办法为按钮设置一个。 有没有办法从数据动态设置按钮样式?甚至可能是纯 XAML 方法? 最佳答案 完成
我正在尝试使用ItemContainerStyleSelector根据定义行的对象的类型在数据网格中显示不同的行样式(ItemsSource是IGridItem的集合,有GridItem和GridSe
我在 XAML 中创建了一个样式,如何在样式选择器(代码)中返回这个样式? 我在 XAML 中创建了样式,我只想返回在 XAML 中声明的样式。 最佳答案 您可以将属性添加到您的 StyleSelec
我有带数据网格的 RowStyleSelector。它在窗口加载时被正确调用。在我调用 PropertyChange 时更改行时它不会被调用。 这是我的模型: public class TraceD
我是一名优秀的程序员,十分优秀!