Mr Bell
Mr Bell

Reputation: 9338

Why doesn't WPF border control have a mousedoubleclick event?

Why doesn't WPF border control have a mousedoubleclick event? I have a ItemsControl with some layout stuff in it for a DataTemplate. I want to handle the double click event to pop up a details dialog, but the border, my layout container, doesn't appear to expose the event.

Any suggestions on how to either get at the double click event, or rework the xaml to make it possible?

Upvotes: 34

Views: 23624

Answers (3)

Jaster
Jaster

Reputation: 8579

Just use InputBindings.

<Border>
    <Border.InputBindings>
        <MouseBinding MouseAction="LeftDoubleClick" Command="..."/>
    </Border.InputBindings>
</Border>

In general; avoid using events if not developing controls in WPF. Usually the usage of code behind based events is a strong indication for a MVVM Pattern break.

Upvotes: 69

Lukasz Madon
Lukasz Madon

Reputation: 14994

Update: Sorry, my bad - late hour

Inside your mouse button down event get ClickCount

 //  e.Handled = true;  optional

 if (e.ClickCount > 1)
 {
    // here comes double click and more :)
 }

Upvotes: 7

John Bowen
John Bowen

Reputation: 24453

MouseDoubleClick is declared on Control so you just need an instance of some Control in your ItemTemplate. The simplest thing to do is use the base Control class which doesn't have any other behavior and just give it a customized template with what's in your ItemTemplate now.

<ItemsControl>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Control MouseDoubleClick="Control_MouseDoubleClick">
                <Control.Template>
                    <ControlTemplate>
                        <Border>
                            <!--Other ItemTemplate stuff-->
                        </Border>
                    </ControlTemplate>
                </Control.Template>
            </Control>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Upvotes: 16

Related Questions