Jakob Lithner
Jakob Lithner

Reputation: 4406

Can not find DoubleClick in XAML

I have a ListView control in XAML. The items are defined by a template control with a TextBlock inside a Grid. Now I want to trigger an action when I double click the TextBlock. To my surprise I find there is no DoubleClick event to hook up ... ! I thought it was just the TextBlock that din't have it, but actually no controls have it. I look for DoubleClick and MouseDoubleClick but they are definitely absent.

I have read suggestions where an EventTrigger is added to a control with Gesture="MouseDoubleClick". It looks promising but in my case the compiler complains and tells me there is no such gesture as a MouseDoubleClick. Same with DoubleClick.

Did the DoubleClick disappear in some version of .Net? I have .Net Framework 4.5 and the project is WPF Application.

Do I have to do stupid workarounds by detecting MouseDown and check the elapsed time since last MouseDown? Sounds like stoneage ...

Upvotes: 20

Views: 14196

Answers (3)

Evgy
Evgy

Reputation: 396

Use content ContentControl, Luke!

<ContentControl MouseDoubleClick="OnDoubleClick"> 
    <TextBlock Text="Hello" FontSize="15" /> 
</ContentControl>

Upvotes: 11

user1665567
user1665567

Reputation:

Just to show the solution Jakob means:

private void img_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && e.ClickCount == 2)
            {
                //DO SOMETHING
            }
        }

Upvotes: 28

Jakob Lithner
Jakob Lithner

Reputation: 4406

It is always refreshing to formulate your problem! After thinking a while I thought maybe they added a counter to the mouse events instead of having separate events. That seems to be the case!!! The MouseButtonEventArgs has a ClickCount property. When checking for value 2 I detect my DoubleClick!

Still a bit odd though to just kick out the DoubleClick. Even after searching I find no reference as to when and why it disappeared.

Upvotes: 35

Related Questions