在XAML样式中,如何将实体背景更改为渐变?

时间:2009-02-06 13:03:19

标签: wpf xaml styles

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

将渐变色作为此样式的背景的正确方法是什么?

1 个答案:

答案 0 :(得分:77)

您错过了<Setter.Value>

<Style ...> 
   <Setter Property="...">
      <Setter.Value>
         <LinearGradientBrush />
      </Setter.Value>
   </Setter>
</Style>