tags:

views:

53

answers:

2

Hi. 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!

+1  A: 

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 article and this

UPDATE to handle both Click and 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

Eugene Cheverda
I didn't really understand what you meant by using ClickCount. Can you please demonstrate it by code? Thanks
amitairos
ClickCount is one of te properties of MouseButtonEventArgs. When you declare event handler, it can be accessed through e.ClickCount. You need to track this value to know whether it was just ONE click or more.if (e.ClickCount == 1)//raise your Click event hereif (e.ClickCount == 2)//raise your DoubleClick event hereIf your handler is Preview (means that raising starts exactly on YOUR control up to the parents) you may be should set in handler e.Handled to true.
Eugene Cheverda
And I'm sorry, I have no installed IDE on this machine now to post to you clear code how to handle it right. :(
Eugene Cheverda
In the event handler I wrote if(e.ClickCount ==1) before the code, but it still doesn't respond to the click.
amitairos
See updated answer. Sorry for poor code, I've used notepad for it...
Eugene Cheverda
Thanks, but I want the event not in the UserControl itself, but when I add it to the window. I want the event handler in the MainWindow.cs
amitairos
See another update for use in MainWindow
Eugene Cheverda
Thanks, it works. I was using the PreviewMouseleftUp event, and your using the down. The problem with this event- is that the event is triggered when the user is just holding the button down- before he releases the button. How can I do that the event will trigger when the user releases the mouse button?
amitairos
Experiment with either Bubbling or Tunnelling (prefixed with Preview) routed events of MouseButtonUp event handlers. And I recommend you to learn up Routed Enents on this link http://msdn.microsoft.com/en-us/library/ms742806.aspx . The idea is clear, you just have to learn and understand it cases of usage.
Eugene Cheverda
To find out whether the button released you can use for example this methodprivate bool GetMouseLeftButtonReleased(){ return (InputManager.Current.PrimaryMouseDevice.LeftButton == MouseButtonState.Released);}
Eugene Cheverda
Thanks! Works perfect!
amitairos
You're welcome ;)
Eugene Cheverda
Sorry. I thought I did it ok.Where to I use the GetMouseLeftButtonReleased?I did if(GetMouseLeftButtonReleased()==true) in the event handler, and it doesn't continue when I release it.
amitairos
Wait for a while, i'm installing VS Express to write right working code.
Eugene Cheverda
Check updated answer. I've just tested it, it handles both Click and DoubleClick correct.
Eugene Cheverda
Thanks. Sorry for all the hassle. It appears to be a problem with my solution. Any ideas why?
amitairos
So what is not working now? The solution is clear for handle Click events, try to debug and find were is the problem, anyway code in answer works, and does it's deal. I can not find problem in your solution because I didn't see it at all.
Eugene Cheverda
I found the problem. It's interfering with the Window's MouseLeftButtonDown event which has a "this.DragMove()" method in it.I want to enable the DragMove since it is a borderless window.Maybe there's a way to disable the dragmove when I am on a control?
amitairos
A: 

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)

  • 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.
Nir