在XAML中,是否可以在不指定TargetType的情况下定义全局常量样式

时间:2011-07-13 17:15:36

标签: xaml

我想将徽标颜色定义为样式,然后将颜色应用于任何地方。像这样:

<Style x:Name="LogoBlue">
   <Setter Property="Background" Value="#607C8C" />
</Style> 

<TextBlock Background="{StaticResource LogoBlue}">Blah Blah</TextBlock>

是否可以将颜色常量定义为静态资源?

1 个答案:

答案 0 :(得分:0)

在App.xaml中将画笔定义为资源,然后您可以通过其键来引用它。

由于颜色是固定颜色,因此PresentationOptions可以更有效地使用画笔,因为您不会改变它的颜色。

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="PresentationOptions"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <SolidColorBrush x:Key="LogoBlue" Color="#607C8C" PresentationOptions:Freeze="True"/>
    </Application.Resources>
</Application>