Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore global style for specific control and it's children

Tags:

c#

wpf

xaml

telerik

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?

like image 973
Marc Avatar asked May 27 '16 20:05

Marc


People also ask

Can I set individual styles in my Global CSS file?

You can set individual styles in your global CSS file as !important overrides inline styles set directly on elements. However, you should avoid using !important, because it makes debugging more difficult by breaking the natural cascading in your stylesheets. Instead of using !Important, you can try the following:

How to override inline styles with the !important rule?

When an important rule is used on a style declaration, this declaration will override any other declarations. When two conflicting declarations with the !important rules are applied to the same element, the declaration with a greater specificity will be applied. Let’s see how you can use the !important declaration to override inline styles.

How do I set the global setting for decorators in a storybook?

Inside the .storybook folder, there is a file named preview.js, which sets the global setting for decorators, parameters, and global types. Import this style file into .storybook/preview.js (line 1):


1 Answers

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)

like image 62
Evk Avatar answered Oct 17 '22 04:10

Evk