忽略特定控件的全局样式及其子项 [英] Ignore global style for specific control and it's children

查看:33
本文介绍了忽略特定控件的全局样式及其子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尽力确保这不是其他问题的完全重复,并尝试了很多可能的解决方案.也许我只是没有进行正确的搜索.

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

根据祖先类型的存在设置样式

使用该问题中的答案,我创建了一个转换器来检查控件是否具有特定类型的祖先.代码与上面的问题几乎相同,所以为了避免这个问题太长,我不会在这里粘贴.

Using the answers in that question I created a converter to check if a control has an ancestor of a specific type. The code is pretty much the same as in the above question so to keep this question from getting too long I won't paste it here.

我修改了我的风格

<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 中使用的许多控件类型,我有类似的触发器.

Although this works, I was concerned about performance as my application grows and there's more and more controls. I have similar triggers for many control types that could be used in the RadGridView.

每个控件都将递归检查它是否具有 RadGridView 作为祖先.大多数不会,所以他们需要一直搜索到他们的基本容器,然后才知道他们没有 RadGridView 作为祖先.

Every control is going to be recursively checking if it has a RadGridView as an ancestor. Most won't, so they need to search all the way up to their base container before knowing they don't have a RadGridView as an ancestor.

所以在那之后,我的问题是是否有更好的方法来做到这一点?有什么我可以用它包装 RadGridView 的东西,它会告诉它,它是子元素,忽略我的资源字典中的样式吗?

So after that the lead up, my questions is whether there's a better way to do this? Is there something I can wrap the RadGridView with that will tell it, and it's child elements, to ignore the styles in my resource dictionary?

推荐答案

是的,有更好的方法来做到这一点:在 RadGridView 本身的 Resources 中定义空样式,或者,如果您想将其应用于所有 RadGridViews - 在 RadGridView 样式本身的资源中定义它,如下所示:

Yes there is better way to do this: define empty style either in Resources of RadGridView itself, or if you want to apply that to all RadGridViews - define it in resources of RadGridView style itself, like this:

<Style TargetType="telerik:RadGridView">
    <Style.Resources>
        <Style TargetType="TextBlock" />
    </Style.Resources>
</Style>

这将阻止 RadGridView 的所有子级继承全局 TextBlock 样式(相反,它们将使用默认"TextBlock 样式)

That will prevent all children of RadGridView to inherit global TextBlock style (instead they will use "default" TextBlock style)

这篇关于忽略特定控件的全局样式及其子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆