WPF CustomControl - 按钮不会触发 Click 事件

WPF CustomControl - Button won't fire Click event

我创建了某种方形按钮作为 CustomControl,其中包含 "re-gain some programming skill"(学习目的)的绑定和内容。

现在我 运行 遇到了我的自定义命令不会被触发的问题。在我检查了所有我能弄清楚的东西之后,我直接测试了点击事件,但即使是点击事件本身也不会被触发。

自定义控件:

<UserControl x:Class="SGDB.Controls.QuadButton"
         x:Name="QButton"
         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:SGDB.Controls"
         mc:Ignorable="d"
         d:DesignHeight="100" d:DesignWidth="100" Margin="2,2,2,2">
<UserControl.Resources>
    <Style TargetType="Button">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                    <GradientStop Color="#003264" Offset="0.25"/>
                    <GradientStop Color="#004896" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                    <GradientStop Color="#FFCD9B" Offset="0.25"/>
                    <GradientStop Color="#FFB769" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="FontWeight" Value="Black"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=IsMouseOver}"  Value="True">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                            <GradientStop Color="#411902" Offset="1"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
<Border BorderThickness="1" BorderBrush="White">
    <Button Width="{Binding Size, ElementName=QButton}" Height="{Binding Size, ElementName=QButton}" Command="{Binding ExecuteCommand, ElementName=QButton}" Click="Button_Click">
        <StackPanel Margin="10">
            <Image Source="{Binding ImageSource, ElementName=QButton}" Width="64" Height="64" HorizontalAlignment="Center" VerticalAlignment="Top"/>
            <TextBlock Text="{Binding DisplayText, ElementName=QButton}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,15,0,0"/>
        </StackPanel>
    </Button>
</Border>

代码隐藏:

public partial class QuadButton : UserControl {



    public ICommand ExecuteCommand {
        get { return (ICommand)GetValue(ExecuteCommandProperty); }
        set { SetValue(ExecuteCommandProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ExecuteCommand.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ExecuteCommandProperty =
        DependencyProperty.Register("ExecuteCommand", typeof(ICommand), typeof(QuadButton), new PropertyMetadata(null));



    public int Size {
        get { return (int)GetValue(SizeProperty); }
        set { SetValue(SizeProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Size.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SizeProperty =
        DependencyProperty.Register("Size", typeof(int), typeof(QuadButton), new PropertyMetadata(null));



    public string ImageSource {
        get { return (string)GetValue(ImageSourceProperty); }
        set { SetValue(ImageSourceProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Source.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ImageSourceProperty =
        DependencyProperty.Register("ImageSource", typeof(string), typeof(QuadButton), new PropertyMetadata(null));



    public string DisplayText {
        get { return (string)GetValue(DisplayTextProperty); }
        set { SetValue(DisplayTextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DisplayTextProperty =
        DependencyProperty.Register("DisplayText", typeof(string), typeof(QuadButton), new PropertyMetadata(null));



    public QuadButton() {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e) {
        MessageBox.Show("Test");
    }
}

视图中使用自定义按钮的命令:

<custom:QuadButton Size="200" DisplayText="Administration" ImageSource="../Pictures/Users.png" ExecuteCommand="{Binding VM.TestButton}"/>

(VM = ViewModel,TestButton = 用于测试按钮的命令)

这是我的错 - 我实现了一些代码使我的 Window 可拖动 - 现在我知道这个方法正在吃掉 MouseDown 事件。