WPF样式继承

8

我有这个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>

如果有帮助的话,这是我定义全局样式的方式。
<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个回答

14
To Combine the application Level + Local Resource 
在本地资源定义中。
 <Style TargetType="TextBlock" BasedOn="{StaticResource StyleA}" >  
                <Setter Property="Margin" Value="4" />  
            </Style>

这将为您提供来自应用程序级别和本地级别的样式。


如果应用程序级别的样式没有名称怎么办? - Jonathan Allen
你只需要像“TextBlock”这样提供BaseTypeName。 - Kishore Kumar
你的意思是像这样<Style TargetType="Button" BasedOn="{StaticResource Button}" >吗?因为它不能编译。 - Jonathan Allen
14
基于="{StaticResource {x:Type Button}}" - Kishore Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接