5

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?

Community
  • 1
  • 1
Marc
  • 973
  • 4
  • 13
  • If this is a `TextBlock` style in ``, then this is the intended behavior. I actually just [answered another question related to this yesterday](http://stackoverflow.com/a/37466355/302677), and I think the answer would apply here as well. Either make your global styles for Control objects only, like `Label`, or move it to `Window.Resources` so it won't cross control boundaries. – Rachel May 27 '16 at 20:09
  • I had a scenario where a style had been defined for a ToolTip, which I was generally happy to apply in all but one instance. In that one instance, I set Style="{x:Null}" for the ToolTip and was then able to specify alternative child content within that ToolTip for a more complex tooltip (comprising title, image & description). Thanks for sharing it @Marc, despite it not working for you. – w5m Apr 30 '19 at 09:36

1 Answers1

5

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)

Evk
  • 98,527
  • 8
  • 141
  • 191
  • I added that style as a test but get the following exception when my application starts : 'System.Windows.ResourceDictionary.DeferrableContent' threw an exception.' – Marc May 27 '16 at 20:18
  • 1
    I've verified it works before posting - your have some different problem. Ensure that you don't have duplicated styles in your resource dictionary (maybe you have already defined a style for RadGridView before). – Evk May 27 '16 at 20:22
  • That was it. I had another style for RadGridView that set a default margin and hadn't removed it. Thanks for this solution. Much more simple than what I was trying previously :) – Marc May 27 '16 at 20:26