按钮用户控件 WPF

Button User Control WPF

我正在尝试创建一个以按钮开头的用户控件库。我已经成功创建了一个 button.xaml,代码如下所示;

<UserControl x:Class="SPECTRE.UserControls.Buttons.button"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:SPECTRE.UserControls.Buttons"
         mc:Ignorable="d" 
         Height="Auto" Width="Auto"
         x:Name="RootElement">
<UserControl.Resources>
    <Style x:Key="DangerButton" TargetType="{x:Type Button}">
        <Setter Property="MinWidth" Value="100"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Height" Value="35"/>
        <Setter Property="FontWeight" Value="SemiBold"/>
        <Setter Property="Background" Value="#F12427"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontSize" Value="15"/>
        <!--<Setter Property="Content" Value="Go"/>-->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        CornerRadius="5">
                        <ContentPresenter
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        TextBlock.Foreground="{TemplateBinding Foreground}"
                        Margin="0,0,0,5"
                        Content="Button Text"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#D6E006"/>
                            <Setter Property="Cursor" Value="Hand"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<StackPanel>
    <!--Content should be dynamic-->
    <Button  Style="{StaticResource DangerButton}" Name="button1"/>
</StackPanel>

我有一个用户控制文件 video.xaml,其中的按钮实例化如下;

<Buttons:button/>

我正在尝试使按钮内容动态化,但我似乎做不好。我试过这个 here 但它不起作用。我需要做什么?

您在那里定义的唯一自定义项是 Style。将 Style 移动到 App.xaml 或另一个资源字典,然后简单地将 UserControl 替换为 Button 元素:

<Button Style="{StaticResource DangerButton}" Name="button1" Content="Content..."/>

另一个选项可能是创建一个自定义的 DangerButton 控件继承自 Button:

public class DangerButton : Button
{
    static DangerButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(DangerButton),
            new FrameworkPropertyMetadata(typeof(DangerButton)));
    }
}

在这种情况下,您应该在名为(按照惯例)themes/generic.xaml.

的资源字典中为控件定义默认值 Style

因此将您当前的 Style 移到那里并从中删除 x:Key="DangerButton"。然后您可以像这样使用自定义控件:

<Buttons:DangerButton Content="Content..." />

如果您选择简单地将 Button 包装在 UserControl 中,您将无法直接从您所在的控件设置 Button 的任何属性消耗 UserControl。然后,您必须将属性添加到 UserControl 并将这些属性绑定到 Button.