WPF用Button绑定线性渐变属性

时间:2013-03-12 08:20:09

标签: wpf wpf-4.0

我想将gradient属性绑定到按钮。我正在使用下面的代码。我能够绑定风格。你能建议我如何绑定linergradientbrush属性??

 <Window.Resources>
    <ResourceDictionary>
        <LinearGradientBrush x:Key="buttonStyleGradient"  EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="White" Offset="0" />
            <GradientStop Color="#FFACC3F5" Offset="1" />
        </LinearGradientBrush>
       <Style x:Key="buttonStyle" TargetType="Button">
           <Setter Property="FontFamily" Value="Vrinda"/>
          <Setter Property="FontSize" Value="24"/>
          <Setter Property="Padding" Value="8,4" />
          <Setter Property="Margin" Value="0" />
       </Style>
    </ResourceDictionary>
</Window.Resources><Button  Style="{StaticResource buttonStyle}" >                  
                <Label>Home</Label>
            </Button>

2 个答案:

答案 0 :(得分:4)

只需将buttonStyleGradient添加到buttonStyle作为Background媒体资源:

<Style x:Key="buttonStyle" TargetType="Button">
  <Setter Property="FontFamily" Value="Vrinda"/>
  <Setter Property="FontSize" Value="24"/>
  <Setter Property="Padding" Value="8,4" />
  <Setter Property="Margin" Value="0" />
  <Setter Property="Background" Value="{StaticResource buttonStyleGradient}" />
</Style>

如果你不想将它添加到样式中,你可以像这样手动输入按钮:

<Button  Style="{StaticResource buttonStyle}" Background="{StaticResource buttonStyleGradient}" >

答案 1 :(得分:2)

您需要一个属性来应用渐变,尝试背景:

<Setter Property="Background" Value="{StaticResource buttonStyleGradient}"/>