将Brush定义为Resource从另一资源获取其颜色

时间:2019-03-29 17:37:44

标签: wpf xaml

我想定义一个从其他资源获取颜色的画笔。就是这样

 <Color x:Key="MyColor">#003C83</Color>
 <Brush x:Key="MyColor.Brush" Color="{StaticResource MyColor}" />

我该怎么做?

1 个答案:

答案 0 :(得分:2)

您应该使用特定类型的Brush,因为它是@elgonzo所说的抽象类...这是一个SolidColorBrush的简单示例:

<Window x:Class="XAMLTest.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:local="clr-namespace:XAMLTest"
                mc:Ignorable="d"
                Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Color x:Key="MyColor">#003C83</Color>
        <SolidColorBrush x:Key="MyColor.Brush" Color="{StaticResource MyColor}" />
    </Window.Resources>
        <Grid>
        <Border BorderBrush="{StaticResource MyColor.Brush}" BorderThickness="5" Background="Yellow" Height="20" Width="100" HorizontalAlignment="Center"/>
    </Grid>
</Window>

结果:

enter image description here

请尝试在MSDN上阅读WPF Brushes Overview ...