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

Edw*_*uay 40 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>
Run Code Online (Sandbox Code Playgroud)

而不是"WhiteSmoke"我希望我的背景是这个渐变:

    <LinearGradientBrush>
        <GradientStop Color="#ccc" Offset="0"/>
        <GradientStop Color="#bbb" Offset="1"/>
    </LinearGradientBrush>
Run Code Online (Sandbox Code Playgroud)

但是下面的尝试导致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>
Run Code Online (Sandbox Code Playgroud)

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

rud*_*ler 81

你错过了 <Setter.Value>

<Style ...> 
   <Setter Property="...">
      <Setter.Value>
         <LinearGradientBrush />
      </Setter.Value>
   </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)