在XAML风格中,如何将纯色背景更改为渐变?
我有一个MainResources.xaml文件,其中我有一个样式,它定义了我的应用程序中每个窗口的外观:
<Style x:Key="MainBorderStyle" TargetType="{x:Type Border}"> <Setter Property="Background" Value="WhiteSmoke" /> <Setter Property="BorderBrush" Value="LightGray" /> <Setter Property="BorderThickness" Value="1" /> <Setter Property="CornerRadius" Value="5" /> <Setter Property="SnapsToDevicePixels" Value="True" /> </Style>
而不是“WhiteSmoke”,我希望我的背景是这个渐变:
<LinearGradientBrush> <GradientStop Color="#ccc" Offset="0"/> <GradientStop Color="#bbb" Offset="1"/> </LinearGradientBrush>
但下面的尝试会导致VS2008告诉我“样式设置器不支持子元素”:
<Style x:Key="MainBorderStyle" TargetType="{x:Type Border}"> <Setter Property="Background"> <LinearGradientBrush> <GradientStop Color="#ccc" Offset="0"/> <GradientStop Color="#bbb" Offset="1"/> </LinearGradientBrush> </Setter> <Setter Property="BorderBrush" Value="LightGray" /> <Setter Property="BorderThickness" Value="1" /> <Setter Property="CornerRadius" Value="5" /> <Setter Property="SnapsToDevicePixels" Value="True" /> </Style>
将渐变颜色作为此样式的背景的正确方法是什么?
您错过了<Setter.Value>
<Style ...> <Setter Property="..."> <Setter.Value> <LinearGradientBrush /> </Setter.Value> </Setter> </Style>