WPF样式继承

时间:2010-01-23 06:05:07

标签: wpf inheritance styles

我有这个XAML。如果我删除StackPanel.Resources部分,我会得到在应用程序级别定义的样式。如果我留下它,那么我只会得到新款式。

如何让它结合本地和全局样式?

<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>

如果它有帮助,这就是我定义globla样式的方式。

<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>

1 个答案:

答案 0 :(得分:14)

To Combine the application Level + Local Resource 

在本地资源定义

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

这将为您提供应用级别以及本地级别的样式