为包含按钮的 WPF 用户控件创建自定义单击事件处理程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/888647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a custom click event handler for a WPF usercontrol which contains a button?
提问by Junior Mayhé
have you ever found a problem when assigning a click event handler for your custom WPF usercontrol with a nested button control? I do.
在为带有嵌套按钮控件的自定义 WPF 用户控件分配单击事件处理程序时,您有没有发现问题?我愿意。
When you put such user control in a main window, let's say Main.xaml, the MouseLeftButtonDown doesn't work, but the PreviewMouseLeftButtonDown works like a charm.
当您将此类用户控件放在主窗口中时,例如 Main.xaml,MouseLeftButtonDown 不起作用,但 PreviewMouseLeftButtonDown 工作得很好。
But imagine yourself telling each developer in your team to use this event when using your usercontrol... Some usercontrols in you library has MouseLeftButtonDown, others PreviewMouseLeftButtonDown.... It's a mess don't you agree?
但是想象一下你告诉团队中的每个开发人员在使用你的用户控件时使用这个事件......你库中的一些用户控件有 MouseLeftButtonDown,其他的 PreviewMouseLeftButtonDown......你不同意吗?
So I've got a solution but I want someone to see if there's some elegant way to create your custom event handler called "Click".
所以我有一个解决方案,但我希望有人看看是否有一些优雅的方式来创建名为“Click”的自定义事件处理程序。
In my usercontrol called CustomButton.xaml.cs, I have so far:
在我的名为 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;
}
In my CustomButton.xaml
在我的 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>
Now in my Main.xaml, the caller, I have:
现在在我的 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>
Explanation:
解释:
in the usercontrol, when you click the nested button, it executes its parent custom "Click" handler.
在用户控件中,当您单击嵌套按钮时,它会执行其父级自定义“单击”处理程序。
Is there a elegant way to accomplish the same effect?
有没有一种优雅的方式来实现同样的效果?
采纳答案by Mark Carpenter
Going off of what mdm20 was saying... Why are you creating a UserControl (a collection of controls grouped into 1) when you could much more easily create a CustomControl (a control that extends the functionality of an existing control, such as a Button)? Assuming a Button is the only control you'd like in CustomButton, I'd highly recommend a CustomControl over what you have (a UserControl).
脱离 mdm20 所说的......当您可以更轻松地创建 CustomControl(扩展现有控件的功能的控件,例如 Button)时,为什么要创建 UserControl(一组控件的集合) )?假设 Button 是您想要在 CustomButton 中的唯一控件,我强烈建议您使用 CustomControl,而不是您拥有的控件(UserControl)。
Example of UserControl vs CustomControl here
此处为 UserControl 与 CustomControl 的示例
Hope this helps!
希望这可以帮助!
回答by mdm20
If your implementing a button, why not just derive from button?
如果您实现一个按钮,为什么不直接从按钮派生?
To answer your question though, all you need it this.
要回答你的问题,你只需要这个。
if (Click != null) Click(this, EventArgs.Empty);
回答by Martin Harris
Couldn't this line:
这行不能:
(((sender as Button).Parent as Grid).Parent as CustomButton).Click(sender, e);
be replaced by
被取代
this.Click(sender, e);
?
?
Other than that though the answer depends on the exact behaviour that you want. If you want to click event of your user control to only trigger when you click on the inner button then I think you are handling it the right way. On the other hand if you want the click event to trigger whenever you click anywhere within the bounds of the user control then you are probably best styling or inheriting from the standard button control. Remember that in WPF the button's content can be any other element including another button.
除此之外,答案取决于您想要的确切行为。如果您只想在单击内部按钮时触发用户控件的单击事件,那么我认为您正在以正确的方式处理它。另一方面,如果您希望单击事件在您单击用户控件范围内的任何位置时触发,那么您可能最好设置样式或从标准按钮控件继承。请记住,在 WPF 中,按钮的内容可以是任何其他元素,包括另一个按钮。