Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

wpf - In XAML style, how to change solid background to gradient?

I've got a MainResources.xaml file in which I have a style that defines how each of my windows in my application look:

  <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>

Instead of "WhiteSmoke" I want my background to be this gradient:

    <LinearGradientBrush>
        <GradientStop Color="#ccc" Offset="0"/>
        <GradientStop Color="#bbb" Offset="1"/>
    </LinearGradientBrush>

But the following attempt causes VS2008 to tell me "Style setters do not support child elements":

<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>

What is the correct way to put a gradient color as the background for this style?

question from:https://stackoverflow.com/questions/520190/in-xaml-style-how-to-change-solid-background-to-gradient

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are missing the <Setter.Value>

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...