Ignore global style for specific control and it's children

半腔热情 提交于 2019-12-10 20:24:42

问题


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.


The problem

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.

So for example, I have the following style for TextBlock

<Style TargetType="TextBlock">
    <Setter Property="Height" Value="26"/>
</Style>

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.

I would like to get the RadGridView, and all of it's child controls, to ignore the styles in my resource dictionary.


I tried this but it didn't work

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

Setting style based on existence of an ancestor type

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.

I modified my style to this

<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>

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.

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.


My question

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?


回答1:


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>

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



来源:https://stackoverflow.com/questions/37491430/ignore-global-style-for-specific-control-and-its-children

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!