WPF样式继承

Jon*_*len 8 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>
Run Code Online (Sandbox Code Playgroud)

如果它有帮助,这就是我定义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>
Run Code Online (Sandbox Code Playgroud)

Kis*_*mar 14

To Combine the application Level + Local Resource 
Run Code Online (Sandbox Code Playgroud)

在本地资源定义中

 <Style TargetType="TextBlock" BasedOn="{StaticResource StyleA}" >  
                <Setter Property="Margin" Value="4" />  
            </Style>
Run Code Online (Sandbox Code Playgroud)

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

  • BasedOn ="{StaticResource {x:Type Button}}" (13认同)