UserControl-WPF 中的单击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3222456/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:55:29  来源:igfitidea点击:

Click event in UserControl- WPF

wpf

提问by amitairos

I have a UserControl in my WPF application. I want to call a click event and do some things when the user clicked the UserControl. The problem is- the UserControl doesn't have a click event. I searched on the web and found that you can use the MouseLeftButtonUp event. I tried it- but it doesn't respond to my clicks. Any ideas? Thanks!

我的 WPF 应用程序中有一个 UserControl。我想在用户单击 UserControl 时调用单击事件并执行一些操作。问题是 - UserControl 没有点击事件。我在网上搜索,发现可以使用 MouseLeftButtonUp 事件。我试过了 - 但它对我的点击没有反应。有任何想法吗?谢谢!

回答by Nir

You didn't write what you are trying to do but if you need a click event maybe you are writing some kind of button (the Button class is actually "something you can click" with the visual representation in a control template you can replace)

您没有编写您要执行的操作,但是如果您需要单击事件,那么您可能正在编写某种按钮(Button 类实际上是“您可以单击的东西”,并且可以替换控件模板中的可视化表示)

  • If you need a button with complex content inside - put your user control inside a button
  • If you need a button that doesn't look like a button write a custom control template for button
  • If you need a button with extra functionality subclass button, add the extra data/behavior in code and put the display XAML in a style.
  • 如果您需要一个包含复杂内容的按钮 - 将您的用户控件放在一个按钮内
  • 如果您需要一个看起来不像按钮的按钮,请为按钮编写自定义控件模板
  • 如果您需要一个具有额外功能子类按钮的按钮,请在代码中添加额外的数据/行为并将显示 XAML 置于样式中。

回答by Eugene Cheverda

I think for your needs PreviewMouseLeftButtonUp(Down) event is more suitable. Then you need to handle ClickCount for counting amount of clicks and then raise your own event, on which other controls will know, that your control is clicked. There are much more methods on handling click event. You should look at this msdn articleand this

我认为根据您的需要 PreviewMouseLeftButtonUp(Down) 事件更合适。然后你需要处理 ClickCount 来计算点击量,然后引发你自己的事件,其他控件会知道你的控件被点击。处理点击事件的方法还有很多。你应该看看这篇 msdn 文章这个

UPDATE to handle both Click and DoubleClick

更新以处理 Click 和 DoubleClick

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        _myCustomUserControl.MouseLeftButtonUp += new MouseButtonEventHandler(_myCustomUserControl_MouseLeftButtonUp);
        _myCustomUserControl.MouseDoubleClick += new MouseButtonEventHandler(_myCustomUserControl_MouseDoubleClick);
    }

    bool _doubleClicked;        

    void _myCustomUserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        _textBlock.Text = "Mouse left button clicked twice";
        _doubleClicked = true;
        e.Handled = true;    
    }

    void _myCustomUserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (_doubleClicked)
        {
            _doubleClicked = false;
            return;
        }

        _textBlock.Text = "Mouse left button clicked once";
        e.Handled = true;           
    }        
}

To test this example name your control as _myCustomUserControl and add a TextBlock named _textBlock to your MainWindow.xaml

要测试此示例,请将您的控件命名为 _myCustomUserControl 并将名为 _textBlock 的 TextBlock 添加到 MainWindow.xaml

回答by Guilherme

Why not just use MouseDown?

为什么不直接使用 MouseDown?

Put the event in the User Control and simply do this:

将事件放在用户控件中,只需执行以下操作:

private void MyControl_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Left)
    {
        MessageBox.Show("Clicked!");
    }
}