Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Style Inheritance

I have this XAML. If I remove the StackPanel.Resources section I get the styles that were defined at the application level. If I leave it in, then I only get the new styles.

How to I make it combine both the local and global styles?

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" >
            <StackPanel.Resources>
                <Style TargetType="TextBlock" >
                    <Setter Property="Margin" Value="4" />
                </Style>
                <Style TargetType="Button" >
                    <Setter Property="Margin" Value="4" />
                </Style>
            </StackPanel.Resources>
            <Border Padding="5" BorderBrush="Blue" BorderThickness="4" >
                <StackPanel>
                    <TextBlock>Applications</TextBlock>
                    <Button>Open Issues</Button>
                    <Button>Services</Button>
                </StackPanel>
            </Border>
        </StackPanel>
        <StackPanel></StackPanel>
    </DockPanel>
</Window>

In case it helps, this is how I defined the globla styles.

<Application x:Class="Application"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary Source="ShinyBlue.xaml"/>
    </Application.Resources>
</Application>
like image 593
Jonathan Allen Avatar asked Jan 23 '10 06:01

Jonathan Allen


1 Answers

To Combine the application Level + Local Resource 

in the local resource definition

 <Style TargetType="TextBlock" BasedOn="{StaticResource StyleA}" >  
                <Setter Property="Margin" Value="4" />  
            </Style>

This will give you style from app level as well the local level

like image 132
Kishore Kumar Avatar answered Sep 28 '22 02:09

Kishore Kumar