key值调用
作者:邱名涛
撰写时间:2019年5月14日
<Window x:Class="WpfApplication1.Demo2"
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:WpfApplication1"
mc:Ignorable="d"
Title="Demo2" Height="300" Width="300">
<Window.Resources>
<!--样式资源-->
<Style x:Key="ButtonStyle" TargetType="Button"> <!--定义的key值可以多处调用-->
<!--背景色-->
<Setter Property="FontSize" Value="24"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush
StartPoint="0,0"
EndPoint="1,1"
MappingMode="RelativeToBoundingBox"><!--MappingMode绘图模型-->
<GradientStop Color="Black" Offset="0"></GradientStop>
<GradientStop Color="White" Offset="1"></GradientStop>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<!--页面布局-->
<Grid>
<!--依赖属性:绑定Style="{StaticResource ButtonStyle}-->
<Button Content="依赖属性测试"
Style="{StaticResource ButtonStyle}"
Height="80"
Margin="0,110,-0.4,80.4">
</Button><!--没有设定颜色调用了key值即可获取key值的元素-->
<!--依赖属性:优先级(属性默认复制优先Background="Yellow" FontSize="39")-->
<Button Content="依赖属性优先级"
Background="Yellow"
FontSize="39"
Style="{StaticResource ButtonStyle}"
Height="80"
Margin="0,190,-0.4,0.4">
</Button><!--设定颜色调用了key值即无效-->
</Grid>
</Window>