Search code examples
c#wpfxamlvisifire

How to tell a element and its childs to ignore a global style


To get a common look & feel in my application, I defined a global style for all my TextBlock elements like that:

MainSkin.xaml

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="Margin" Value="5,0,5,0"/>
</Style>

Unfortunately this messes up a chart element (from external visifire library) I use and clippes some of the text elements (marked with red rectangle in screenshot):

enter image description here

View.xaml

xmlns:vc="clr-namespace:Visifire.Charts;assembly=WPFVisifire.Charts"

<vc:Chart>
    <vc:Chart.Titles>
        <vc:Title Text="SomeTitle" />
    </vc:Chart.Titles>
    <vc:Chart.AxesX>
        <vc:Axis Title="X" TitleFontSize="12" />
    </vc:Chart.AxesX>
    <vc:Chart.AxesY>
        <vc:Axis Title="Y" TitleFontSize="12"/>
    </vc:Chart.AxesY>
</vc:Chart>

From testing I know the Margin setting of the TextBlock style is causing this. I guess somewhere inside the Chart element, they use a TextBlock that my style affects.

How can I tell the Chart element and its childs in Xaml to ignore the global TextBlock style?

I tried setting the Chart style to null, without success.

Caveats:

  • I don't want to edit external source code used in the Chart
  • I don't want to use a x:Key with my style

Solution

  • Just create another TextBlock implicit style that doesn't set anything:

    <vc:Chart>
        <vc:Chart.Resources>
            <Style TargetType="TextBlock" />
        </vc:Chart.Resources>
    
        <vc:Chart.Titles>
            <vc:Title Text="SomeTitle" />
        </vc:Chart.Titles>
        <vc:Chart.AxesX>
            <vc:Axis Title="X" TitleFontSize="12" />
        </vc:Chart.AxesX>
        <vc:Chart.AxesY>
            <vc:Axis Title="Y" TitleFontSize="12"/>
        </vc:Chart.AxesY>
    </vc:Chart>