none
Label MouseClick?

    Question

  • Hi,

    I'm taking another stab at WPF. I would like to get the equivalent behavior of a Click event from a Label. The only way I can currently see how to achieve that (kind of) is to register for both MouseDown and MouseUp, storing information about the MouseDown event and checking it on MouseUp, but that will fail in a scenario like this:

    1. MouseDown on label

    2. move mouse outside and release button

    3. MouseDown outside

    4. move mouse inside label and release (= false positive)

     

    Either way, having to handle both MouseDown and MouseUp for something as simple as a MouseClick seems strange.

     

    What to do? Thanks.

    Friday, March 28, 2008 1:43 PM

Answers

  • MouseClick is not such a simple thing Smile

     

    1. Example Window XAML:

     

    Code Snippet

    <Window x:Class="ClickExample.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="clr-namespace:ClickExample"

    Title="ClickExample" Height="300" Width="300">

    <StackPanel>

    <local:ClickableLabel Target="{Binding ElementName=NameBox}"

    Click="Label_Click">Na_me:</local:ClickableLabel>

    <TextBox MinWidth="150"

    x:Name="NameBox">John Smith</TextBox>

    <TextBox MinWidth="150"

    x:Name="Log"/>

    </StackPanel>

    </Window>

     

     

    2. Example Window code-behind:

     

    Code Snippet

    using System;

    using System.Windows;

     

    namespace ClickExample

    {

    public partial class Window1 : System.Windows.Window

    {

    public Window1()

    {

    InitializeComponent();

    }

    void Label_Click(object sender, RoutedEventArgs e)

    {

    Log.Text += "\r\n" + "Label click at " + DateTime.Now;

    }

    }

    }

     

     

    3. Label Click event implementation:

     

    Code Snippet

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Controls.Primitives;

    using System.Windows.Input;

     

    namespace ClickExample

    {

    public class ClickableLabel : Label

    {

    public static readonly RoutedEvent ClickEvent;

     

    static ClickableLabel()

    {

    ClickEvent = ButtonBase.ClickEvent.AddOwner(typeof(ClickableLabel));

    }

     

    public event RoutedEventHandler Click

    {

    add { AddHandler(ClickEvent, value); }

    remove { RemoveHandler(ClickEvent, value); }

    }

     

    protected override void OnMouseDown(MouseButtonEventArgs e)

    {

    base.OnMouseDown(e);

    CaptureMouse();

    }

     

    protected override void OnMouseUp(MouseButtonEventArgs e)

    {

    base.OnMouseUp(e);

    if (IsMouseCaptured)

    {

    ReleaseMouseCapture();

    if (IsMouseOver)

    RaiseEvent(new RoutedEventArgs(ClickEvent, this));

    }

    }

    }

    }

     

     

    Saturday, March 29, 2008 6:52 PM

All replies

  • Have you tried retemplating controls that natively expose mouse input events?

     

    You can try retemplating for example a button to look like a label.

    Friday, March 28, 2008 2:10 PM
  • Thanks, that could be a way... but does that mean I generally don't have full mouse control over my controls, only certain types? That I will need to either inherit from, or skin, a Button every time I need mouse control? Doesn't seem right.

     

    (BTW, is the Alert Me function supposed to work? I've never received any mails...)

    Friday, March 28, 2008 4:17 PM
  • MouseClick is not such a simple thing Smile

     

    1. Example Window XAML:

     

    Code Snippet

    <Window x:Class="ClickExample.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="clr-namespace:ClickExample"

    Title="ClickExample" Height="300" Width="300">

    <StackPanel>

    <local:ClickableLabel Target="{Binding ElementName=NameBox}"

    Click="Label_Click">Na_me:</local:ClickableLabel>

    <TextBox MinWidth="150"

    x:Name="NameBox">John Smith</TextBox>

    <TextBox MinWidth="150"

    x:Name="Log"/>

    </StackPanel>

    </Window>

     

     

    2. Example Window code-behind:

     

    Code Snippet

    using System;

    using System.Windows;

     

    namespace ClickExample

    {

    public partial class Window1 : System.Windows.Window

    {

    public Window1()

    {

    InitializeComponent();

    }

    void Label_Click(object sender, RoutedEventArgs e)

    {

    Log.Text += "\r\n" + "Label click at " + DateTime.Now;

    }

    }

    }

     

     

    3. Label Click event implementation:

     

    Code Snippet

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Controls.Primitives;

    using System.Windows.Input;

     

    namespace ClickExample

    {

    public class ClickableLabel : Label

    {

    public static readonly RoutedEvent ClickEvent;

     

    static ClickableLabel()

    {

    ClickEvent = ButtonBase.ClickEvent.AddOwner(typeof(ClickableLabel));

    }

     

    public event RoutedEventHandler Click

    {

    add { AddHandler(ClickEvent, value); }

    remove { RemoveHandler(ClickEvent, value); }

    }

     

    protected override void OnMouseDown(MouseButtonEventArgs e)

    {

    base.OnMouseDown(e);

    CaptureMouse();

    }

     

    protected override void OnMouseUp(MouseButtonEventArgs e)

    {

    base.OnMouseUp(e);

    if (IsMouseCaptured)

    {

    ReleaseMouseCapture();

    if (IsMouseOver)

    RaiseEvent(new RoutedEventArgs(ClickEvent, this));

    }

    }

    }

    }

     

     

    Saturday, March 29, 2008 6:52 PM
  • You can use Prism with Behavio ...
    Thursday, November 24, 2011 5:42 PM
  • Thanks a lot, just right on the target

    Primary platform is Windows 7 Ultimate 64 bit along with VS 2012/Sql2k8 for WPF/SilverLight projects.

    Wednesday, January 08, 2014 10:02 PM
  • Just my two cents worth here, without writing any extra code the Label_MouseDown event fires when you click a label.

    I have a set of radio buttons and I use labels instead of the Content of the radio button so I can position them separately and the Mouse Down event works if a user clicks the label instead of the radio button, I can select the radio button in the Mouse Down event.

     
    Tuesday, June 03, 2014 1:57 PM