【问题标题】:Create Rounded Toggle Button in WPF在 WPF 中创建圆角切换按钮
【发布时间】:2015-10-14 17:50:02
【问题描述】:

我想创建具有 CornerRadius DependencyProperty 的 ToggleButton,该属性绑定到标准模板中的 Borders' CornerRadius。我的步骤:

在 Blend 中,我只是将 ToggleButton 放在 MainWindow 上,然后使用上下文菜单选择“Make Into UserControl”。在后面的代码中,我创建了 DependencyProperty CornerRadius:

public partial class RoundedToggleButton : UserControl
{
    public RoundedToggleButton()
    {
        InitializeComponent();   
    }

    [Category("Appearance")]
    public CornerRadius CornerRadius
    {
        get { return (CornerRadius)GetValue(CornerRadiusProperty); }
        set { SetValue(CornerRadiusProperty, value); }
    }

    public static readonly DependencyProperty CornerRadiusProperty =
        DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(RoundedToggleButton), new PropertyMetadata(new CornerRadius()));
}

然后我使用“编辑模板 -> 编辑副本..”创建了模板,并将它与我的 UserControl 定义放在同一个地方并添加了模板绑定:

<UserControl x:Class="SqlDeploymentTool.RoundedToggleButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="clr-namespace:SqlDeploymentTool"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         d:DesignHeight="19.96"
         d:DesignWidth="76.257"
         mc:Ignorable="d">
<UserControl.Resources>
    <Style x:Key="FocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="2"
                               SnapsToDevicePixels="true"
                               Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                               StrokeDashArray="1 2"
                               StrokeThickness="1" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD" />
    <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070" />
    <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD" />
    <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1" />
    <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6" />
    <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B" />
    <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4" />
    <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5" />
    <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383" />
    <Style x:Key="RoundedToggleButtonStyle" TargetType="{x:Type ToggleButton}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />
        <Setter Property="Background" Value="{StaticResource Button.Static.Background}" />
        <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}" />
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Padding" Value="1" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border x:Name="border"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            CornerRadius="{TemplateBinding local:RoundedToggleButton.CornerRadius}"
                            SnapsToDevicePixels="true">
                        <ContentPresenter x:Name="contentPresenter"
                                          Margin="{TemplateBinding Padding}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Focusable="False"
                                          RecognizesAccessKey="True"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Button.IsDefaulted" Value="true">
                            <Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="border" Property="Background" Value="{StaticResource Button.MouseOver.Background}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.MouseOver.Border}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="border" Property="Background" Value="{StaticResource Button.Pressed.Background}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.Pressed.Border}" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="border" Property="Background" Value="{StaticResource Button.Disabled.Background}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.Disabled.Border}" />
                            <Setter TargetName="contentPresenter" Property="TextElement.Foreground" Value="{StaticResource Button.Disabled.Foreground}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<Grid>
    <ToggleButton x:Name="toggleButton"
                  Content="ToggleButton"
                  Style="{DynamicResource RoundedToggleButtonStyle}" />

</Grid>

在属性窗口中,我得到了 CornerRadius 属性,但它不受按钮边框的影响。我该如何解决这个问题?

【问题讨论】:

    标签: c# wpf user-controls rounded-corners togglebutton


    【解决方案1】:

    说实话,制作自定义控件而不是 UserControl 会比这更简单直接。

    创建一个名为RoundedToggleButton 的类,该类继承自ToggleButton,并在其上包含您的CornerRadius DependencyProperty。然后创建一个默认样式(没有x:Key,只有TargetType),与您在那里的样式类似,但将所有TargetTypes 更改为RoundedToggleButton 而不是ToggleButton

    这样的话,这条线……

    CornerRadius="{TemplateBinding local:RoundedToggleButton.CornerRadius}"
    

    ... 很简单:

    CornerRadius="{TemplateBinding CornerRadius}"
    

    最重要的是,它会起作用!

    但是由于您已经创建了 UserControl,只需将该行更改为:

    CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource 
                           AncestorType={x:Type local:RoundedToggleButton}}}"
    

    【讨论】:

    • 非常感谢,伙计!这真的很有帮助))现在它就像一个魅力))
    • 不客气! :) 但实际上,考虑一下自定义控件的想法...使用当前的方法,您必须为要修改的内部 ToggleButton 的每个属性公开新的 DependencyProperties。例如,上面的文本...现在它被卡在“ToggleButton”中,因为这是您在UserControl 中设置的Content,您将无法从视图的XAML 中更改它。
    • 当然,第一种方法最适合我的情况。我已经将 RoundedToggleButton 创建为 CustomControl,这正是我想要的。再次感谢您的帮助!!!
    猜你喜欢
    • 2015-10-04
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2019-06-09
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    相关资源
    最近更新 更多