问题描述
我已尽力确保这不是其他问题的完全重复,并尝试了很多可能的解决方案.也许我只是没有进行正确的搜索.
I've done my best to ensure this isn't an exact duplicate of other questions and have tried quite a few possible solutions. Maybe I'm just not doing the right searches.
我有一个带有一堆默认样式的资源字典.例如,大多数控件类型的默认高度为 26,以在我的布局中提供一些一致性.
I have a resource dictionary with a bunch of default styles. For example, most control types have a default height of 26 to provide some consistency in my layout.
例如,我为 TextBlock 设置了以下样式
So for example, I have the following style for TextBlock
<Style TargetType="TextBlock">
<Setter Property="Height" Value="26"/>
</Style>
我遇到的问题是 Telerik RadGridView 使用 TextBlock 控件来显示列标题文本,而这些控件采用默认样式,最终高度为 26.
The problem I have is that the Telerik RadGridView uses TextBlock controls to display the column header text and these controls are adopting the default style and end up with a height of 26.
我想获取 RadGridView 及其所有子控件,以忽略资源字典中的样式.
I would like to get the RadGridView, and all of it's child controls, to ignore the styles in my resource dictionary.
我发现了很多建议,可以将要忽略全局样式的控件的样式设置为 Null.我尝试了以下操作,但没有奏效,因为它似乎不适用于 RadGridView 内的子控件.
I found quite a few suggestions to set the style to Null for controls that you want to ignore global styles. I tried the following but it didn't work as it doesn't seem to apply to the child controls inside of the RadGridView.
<telerik:RadGridView Style="{x:Null}">
...
</telerik:RadGridView>
这可行,但可能不是最佳解决方案
我发现了以下问题,其中有一个我可以修改和使用的解决方案
This works but may not be the best solution
I found the following question which had a solution I was able modify and use
使用该问题中的答案,我创建了一个转换器来检查控件是否具有特定类型的祖先.代码与上面的问题几乎相同,所以为了避免这个问题太长,我不会在这里粘贴.
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource HasAncestorTypeConverter},
ConverterParameter={x:Type telerik:RadGridView}}"
Value="False">
<Setter Property="Height" Value="26"/>
</DataTrigger>
</Style.Triggers>
</Style>
虽然这有效,但随着我的应用程序的增长和越来越多的控件,我担心性能.对于可以在 RadGridView 中使用的许多控件类型,我有类似的触发器.
每个控件都将递归检查它是否具有 RadGridView 作为祖先.大多数不会,所以他们需要一直搜索到他们的基本容器,然后才知道他们没有 RadGridView 作为祖先.
所以在那之后,我的问题是是否有更好的方法来做到这一点?有什么我可以用它包装 RadGridView 的东西,它会告诉它,它是子元素,忽略我的资源字典中的样式吗?
推荐答案
<Style TargetType="telerik:RadGridView">
<Style.Resources>
<Style TargetType="TextBlock" />
</Style.Resources>
</Style>
这将阻止 RadGridView
的所有子级继承全局 TextBlock
样式(相反,它们将使用默认"TextBlock
样式)
这篇关于忽略特定控件的全局样式及其子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!