首页 > 解决方案 > 如何以通用方式更改按钮的圆度

问题描述

我目前正在努力使我的 WPF 应用程序更通用一点。到目前为止,对于我想要创建的每个按钮,我都使用了不同的样式来修改圆度(这会创建很多无用的代码)。

使用以下代码,我设法创建了一个可以从 XAML 文件更改的变量,但我无法将其链接到圆度本身。

谁能告诉我我做错了什么?我已经检查了很多论坛,但除了“不要以通用方式做”之外,似乎没有人有答案。

我可以确定一切都在编译,并且样式已正确应用于按钮(不存在 xaml 链接问题)。

我使用的风格:

<Style x:Key="AwakeButton" TargetType="{x:Type customcontrols:AwakeButton}" BasedOn="{StaticResource {x:Type Button}}"
       xmlns:extensions="Awake.Services.Properties:Extensions">
    <Setter Property="customcontrols:AwakeButton.BorderRoundness" Value="4.0"/>
    <Style.Resources>
        <Style TargetType="Border">
            <Setter Property="CornerRadius" Value="{Binding Path=BorderRoundness}" />
            <!--<Setter Property="CornerRadius" Value="10" />-->
        </Style>
    </Style.Resources>
</Style>

我为此创建的按钮的重载:

public class AwakeButton : Button
{
    public AwakeButton()
    {
        
    }

    public static DependencyProperty BorderRoundnessProperty =
         DependencyProperty.RegisterAttached("BorderRoundness", typeof(double), typeof(AwakeButton)); 
    public static void SetBorderRoundness(UIElement element, double value)
    {
        element.SetValue(BorderRoundnessProperty, value);
        
    }

    public static double GetBorderRoundness(UIElement element)
    {
        return (double)element.GetValue(BorderRoundnessProperty);
    }
}

我如何在页面中使用它:

<customcontrols:AwakeButton Style="{StaticResource AwakeButton}" Margin="142,115,0,0"  Width="136" Height="167" BorderRoundness="5">

标签: c#wpfxamldependency-properties

解决方案


您必须将 绑定BorderRoundness到 parent AwakeButton,否则将使用DataContext不包含此属性的 current 解析。此外,如果您从 派生Button,则不必附加依赖属性,您可以使用该Register(...)方法注册一个普通属性。还制作 DPstatic readonly.

<Setter Property="CornerRadius" Value="{Binding BorderRoundness, RelativeSource={RelativeSource AncestorType={x:Type local:AwakeButton}}}" />

如果您不更改按钮的任何特殊内容,您还可以创建附加属性而不是仅用于公开BorderRoundness属性的专用子类型。

public static class ButtonProperties
{
   public static readonly DependencyProperty BorderRoundnessProperty =
      DependencyProperty.RegisterAttached("BorderRoundness", typeof(double), typeof(ButtonProperties));

   public static void SetBorderRoundness(UIElement element, double value)
   {
      element.SetValue(BorderRoundnessProperty, value);

   }

   public static double GetBorderRoundness(UIElement element)
   {
      return (double)element.GetValue(BorderRoundnessProperty);
   }
}

您可以参考BorderRoundness使用附加属性绑定语法(括号)。

<Style x:Key="AwakeButton" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
   <Setter Property="local:ButtonProperties.BorderRoundness" Value="4.0"/>
   <Style.Resources>
      <Style TargetType="Border">
         <Setter Property="CornerRadius" Value="{Binding (local:ButtonProperties.BorderRoundness), RelativeSource={RelativeSource AncestorType={x:Type Button}}}" />
      </Style>
   </Style.Resources>
</Style>

您现在使用带有新创建的附加边框圆度属性的常规按钮。

<Button Grid.Row="0" Style="{StaticResource AwakeButton}" Margin="142,115,0,0"  Width="136" Height="167" local:ButtonProperties.BorderRoundness="5"/>

推荐阅读