2013-03-12 102 views
2

我想将渐变属性绑定到按钮。我使用下面的代码。我能够绑定风格。你能建议我应该如何绑定linergradientbrush属性?带有按钮的WPF绑定线性渐变属性

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

回答

4

只需添加buttonStyleGradientbuttonStyleBackground属性:

<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}" > 
2

你需要的属性应用梯度,尽量背景:

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