c# - 如何以通用方式更改按钮的圆度
问题描述
我目前正在努力使我的 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">
解决方案
您必须将 绑定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"/>
推荐阅读
- javascript - Vue-echarts、条件渲染和下推自动机结合导致问题?
- typescript - 在 TypeScript 中是否可以定义遵循模式的动态属性?
- python - 在 Adobe Acrobat Reader DC 中找不到“选择文件”按钮
- google-analytics - Google / Firebase Analytics 获取应用程序使用高峰时间
- node.js - MongoDB 在从 Kubernetes 集群内部而不是从外部调用时忽略身份验证
- excel - 通过 VBA 更新 Excel 数据透视表范围获取太多行
- c# - 如何在我的 2d 游戏中统一解决这个移动问题?
- android - Kotlin 我对 BLE ScanCallback 有问题
- reactjs - 当使用 map 函数创建多个路由时,相同的组件总是在它有键时卸载
- javascript - 对象解构绑定