wpf - 为包含按钮的 WPF 用户控件创建自定义单击事件处理程序?

标签 wpf user-controls click event-handling

在为带有嵌套按钮控件的自定义 WPF 用户控件分配单击事件处理程序时,您是否发现过问题?我愿意。

当您将这样的用户控件放在主窗口中时,比方说 Main.xaml,MouseLeftButtonDown 不起作用,但 PreviewMouseLeftButtonDown 工作得很好。

但是想象一下,您告诉团队中的每个开发人员在使用您的用户控件时使用此事件...您库中的某些用户控件具有 MouseLeftButtonDown,其他用户控件具有 PreviewMouseLeftButtonDown...。您同意吗?

所以我有一个解决方案,但我希望有人看看是否有一些优雅的方法来创建名为“Click”的自定义事件处理程序。

在我名为 CustomButton.xaml.cs 的用户控件中,到目前为止:

public partial class CustomButton: UserControl
{

    public CustomButton()
        : base()
    {
        this.InitializeComponent();

    }

    public delegate void ClickHandler(object sender, EventArgs e);
    public event EventHandler Click;

    public void Button_Click(object sender, RoutedEventArgs e) {//execute daddy's button click
            (((sender as Button).Parent as Grid).Parent as CustomButton).Click(sender, e);

            e.Handled = false;

    }

在我的 CustomButton.xaml 中

<UserControl
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="YourCompany.UI.Controls.CustomButton" d:DesignHeight="72.5" d:DesignWidth="200">
<UserControl.Resources>
blablabla
</UserControl.Resources>

<Grid x:Name="LayoutRoot">
    <Button Style="{DynamicResource CustomButton}" 
            Width="{Binding ElementName=CustomButton, Path=ActualWidth}" 
            Cursor="Hand" Foreground="#ffffff" FontSize="28" Margin="8,8,0,12" 
            HorizontalAlignment="Left" 
            Content="Custom Button" Click="Button_Click" />


</Grid>

现在在我的 Main.xaml 中,调用者,我有:

<Window x:Class="YourCompany.MyProject.Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyProject!" Height="600" Width="800" 
MinWidth="800" MinHeight="600" WindowState="Maximized" WindowStartupLocation="CenterScreen"
xmlns:bigbola="clr-namespace:YourCompany.UI.Controls;assembly=YourCompany.UI.Controls">

<mycontrols:CustomButton Name="test" MyImage=".\Images\btnOptions.png" Cursor="Hand" 
            Texto="See options" Click="test_Click"
            Margin="168.367,176.702,253.609,0" ToolTip="See all options" Height="76.682" 
            VerticalAlignment="Top"></mycontrols:CustomButton>

解释:

在用户控件中,当您单击嵌套按钮时,它会执行其父自定义“单击”处理程序。

是否有一种优雅的方式来实现同样的效果?

最佳答案

脱离 mdm20 所说的...当您可以更轻松地创建 CustomControl(扩展现有控件功能的控件,例如作为按钮)?假设 Button 是您在 CustomButton 中唯一想要的控件,我强烈建议您使用 CustomControl(UserControl)。

UserControl 与 CustomControl 的示例 here

希望这对您有所帮助!

关于wpf - 为包含按钮的 WPF 用户控件创建自定义单击事件处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/888647/

相关文章:

c# - 是否可以使用 C#/WPF 控制 Telerik RadPropertyGrid 的可见性?

c# - 在 WPF 的设计模式下获取源目录的路径

wpf - 从WPF中的UserControl继承

c# - 类似于 WPF ItemsControl 的用户控件列表

c#.net 使用向导控件从动态框保持控件状态

javascript - 单击任何 div 之外的空白区域时在 jquery 中触发事件

wpf - 查找 FlowDocument 中的所有图像

c# - 为什么我的抽象基 Controller 中的 User(如 User.Identity.Name)为 null?

javascript - 如何在框架中的元素上使用 javascript

HTML 图像嵌入链接在 wordpress 中不起作用