3

在为带有嵌套按钮控件的自定义 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>

解释:

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

有没有一种优雅的方式来达到同样的效果?

4

3 回答 3

2

偏离 mdm20 所说的内容......当您可以更轻松地创建 CustomControl(扩展现有控件功能的控件,例如按钮时,为什么要创建 UserControl(分组为 1 的控件集合) )? 假设 Button 是您在 CustomButton 中想要的唯一控件,我强烈建议您使用 CustomControl 来代替您拥有的东西(UserControl)。

UserControl 与 CustomControl 的示例在这里

希望这可以帮助!

于 2009-05-20T15:49:40.387 回答
1

如果您实现一个按钮,为什么不直接从按钮派生?

不过,要回答您的问题,您只需要这个。

if (Click != null) Click(this, EventArgs.Empty);
于 2009-05-20T15:42:16.750 回答
0

这条线不能:

(((sender as Button).Parent as Grid).Parent as CustomButton).Click(sender, e);

被替换为

this.Click(sender, e);

?

除此之外,尽管答案取决于您想要的确切行为。如果您想单击用户控件的事件以仅在单击内部按钮时触发,那么我认为您以正确的方式处理它。另一方面,如果您希望在单击用户控件范围内的任何位置时触发单击事件,那么您可能是最好的样式或从标准按钮控件继承。请记住,在 WPF 中,按钮的内容可以是任何其他元素,包括另一个按钮。

于 2009-05-20T15:40:34.240 回答