maxp
maxp

Reputation: 25141

Prevent style inheritance

The ToolTip from the following XAML is showing with a red background (as a result of the 'global' Background style property set on TextBlock.

Coming from an HTML/CSS background I'm struggling to see if it's possible to make the ToolTip use a different background (or any other customization for that matter) without explicitly setting a custom Style key on every TextBlock inside a ToolTip

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background" Value="Red"></Setter>
        </Style>
        <Style TargetType="{x:Type Rectangle}">
            <Setter Property="TextBlock.Background" Value="Green"></Setter>
            <Setter Property="Width" Value="20"></Setter>
            <Setter Property="Height" Value="20"></Setter>
            <Setter Property="Fill" Value="Yellow"></Setter>
        </Style>
    </StackPanel.Resources>
    <TextBlock>Hello World</TextBlock>
    <Rectangle>
        <Rectangle.ToolTip>
            <TextBlock>My Tooltip</TextBlock>
        </Rectangle.ToolTip>
    </Rectangle>
</StackPanel>

Upvotes: 2

Views: 1320

Answers (3)

Salah Akbari
Salah Akbari

Reputation: 39946

You just need to move the following line:

<Setter Property="TextBlock.Background" Value="Green"></Setter>

to <Style.Resources> in Rectangle's Style. Like this:

<StackPanel.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Background" Value="Red"></Setter>
    </Style>
    <Style TargetType="{x:Type Rectangle}">
        <Style.Resources>
            <Style TargetType="TextBlock" >
                <Setter Property="Background" Value="Green"></Setter>
            </Style>
        </Style.Resources>

        <Setter Property="Width" Value="20"></Setter>
        <Setter Property="Height" Value="20"></Setter>
        <Setter Property="Fill" Value="Yellow"></Setter>
    </Style>
</StackPanel.Resources>

Upvotes: 1

Evk
Evk

Reputation: 101453

You can add empty style for TextBlock to rectangle resources, like this:

<Rectangle>
    <Rectangle.Resources>
        <Style TargetType="TextBlock" />
    </Rectangle.Resources>
    <Rectangle.ToolTip>
        <TextBlock>My Tooltip</TextBlock>
    </Rectangle.ToolTip>
</Rectangle>

If you want to apply that to all rectangles - do that in your rectangle style:

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background"
                    Value="Red"></Setter>
        </Style>
        <Style TargetType="{x:Type Rectangle}">
            <Style.Resources>
                <Style TargetType="TextBlock" />
            </Style.Resources>
            <Setter Property="TextBlock.Background"
                    Value="Green"></Setter>
            <Setter Property="Width"
                    Value="20"></Setter>
            <Setter Property="Height"
                    Value="20"></Setter>
            <Setter Property="Fill"
                    Value="Yellow"></Setter>
        </Style>
    </StackPanel.Resources>
    <TextBlock>Hello World</TextBlock>
    <Rectangle>
        <Rectangle.ToolTip>
            <TextBlock>My Tooltip</TextBlock>
        </Rectangle.ToolTip>
    </Rectangle>
</StackPanel>

Upvotes: 1

StepUp
StepUp

Reputation: 38094

Am I right that you want to set custom Background or other properties for various ToolTip's? If it is what you want, let me show an example:

<Rectangle>
        <Rectangle.ToolTip>
            <ToolTip>
                <ToolTip.Style>
                    <Style TargetType="ToolTip" BasedOn="{StaticResource {x:Type ToolTip}}">
                        <Setter Property="Background" Value="Red" />
                    </Style>
                </ToolTip.Style>
                <TextBlock>My Tooltip</TextBlock>
            </ToolTip>
        </Rectangle.ToolTip>
    </Rectangle>

Upvotes: 0

Related Questions