添加参数到样式

时间:2011-11-28 21:47:23

标签: wpf

我正在尝试创建一个简单的按钮:

<Style x:Key="ButtonStyle" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border CornerRadius="0" BorderThickness="1" BorderBrush="#83a5d2" x:Name="border">
                    <Border.Background>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                            <GradientStop Color="#e8f1fe" Offset="0"/>
                            <GradientStop Color="#cbe1fe" Offset="0.5"/>
                            <GradientStop Color="#a3c7f1" Offset="0.50"/>
                            <GradientStop Color="#cceffe" Offset="1"/>
                        </LinearGradientBrush>
                    </Border.Background>
                    <ContentPresenter VerticalAlignment="Center" x:Name="contentPresenter"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

但我怎样才能改变CornerRadius?我希望能够将属性传递给对象,以便对象具有该角半径。我无法使用模板绑定,因为Button没有名为CornerRadius的属性。

2 个答案:

答案 0 :(得分:3)

您可以直接在样式中使用“Border.CornerRadius”属性。为了简单起见,我跳过了大多数其他属性:

 <Style x:Name="myBtnStyle" TargetType="{x:Type Button}">
     <Setter Property="FontWeight" Value="Normal" />
     <Setter Property="Border.CornerRadius" Value="3" />
     <!-- more... -->
     <Setter Property="Template">
          <Setter.Value>
              <ControlTemplate TargetType="{x:Type Button}">
              <Border x:Name="bdContent" CornerRadius="{TemplateBinding Border.CornerRadius}">
                      <ContentPresenter Content="{TemplateBinding Content}" />
                  </Border>
              </ControlTemplate>
           </Setter.Value>
      </Setter>
  </Style>

答案 1 :(得分:1)

要将角半径绑定到要以编程方式设置的值,可以在Xaml中声明它...

        <Style x:Key="ButtonStyle"  TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border CornerRadius="{Binding MyCornerRadius}" BorderThickness="1" BorderBrush="#83a5d2" x:Name="border">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Color="#e8f1fe" Offset="0"/>
                                    <GradientStop Color="#cbe1fe" Offset="0.5"/>
                                    <GradientStop Color="#a3c7f1" Offset="0.50"/>
                                    <GradientStop Color="#cceffe" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.Background>
                            <ContentPresenter VerticalAlignment="Center" x:Name="contentPresenter"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

然后创建按钮......

        <Button Style="{StaticResource ButtonStyle}" Content="Hello" DockPanel.Dock="Top" />

然后在你的代码中,你可以设置它......

    public CornerRadius MyCornerRadius { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        MyCornerRadius = new CornerRadius(5, 6, 6, 5);            
    }

这会产生你想要的效果。