Search code examples
c#wpfxamlgraphviz

How to make a DataTemplate respond to a double-click in WPF?


I have a DataTemplate I've defined in my XAML which I'm using to display graph nodes in conjuction with GraphViz4Net. I'd like to be able to take an action when a user double-clicks a vertex. Using their example code, I can change the style of a vertex when it's moused-over:

<DataTemplate DataType="{x:Type local:DocumentPart}">
        <Border BorderBrush="Black" BorderThickness="1" Padding="0" CornerRadius="5" Background="White">
            <Border.Style>
                <Style>
                    <Style.Triggers>
                        <Trigger Property="Border.IsMouseOver" Value="True">
                            <Setter Property="Border.Effect">
                                <Setter.Value>
                                    <DropShadowEffect BlurRadius="2" Color="#BBBBBB" 
                                                        Opacity="0.3" Direction="315"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <StackPanel Orientation="Horizontal">
                <StackPanel Orientation="Vertical" Margin="2">
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </StackPanel>
        </Border>
    </DataTemplate>

How do I handle double-click events?


Solution

  • You could use a MouseBinding:

    <DataTemplate DataType="{x:Type local:DocumentPart}">
        <Border BorderBrush="Black" BorderThickness="1" Padding="0" CornerRadius="5" Background="White">
            <Border.InputBindings>
                <MouseBinding MouseAction="LeftDoubleClick"
                              Command="{Binding YourBindableCommand}"
            </Border.InputBindings>
            <Border.Style>
                <Style>
                    <Style.Triggers>
                        <Trigger Property="Border.IsMouseOver" Value="True">
                            <Setter Property="Border.Effect">
                                <Setter.Value>
                                    <DropShadowEffect BlurRadius="2" Color="#BBBBBB" 
                                                    Opacity="0.3" Direction="315"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <StackPanel Orientation="Horizontal">
                <StackPanel Orientation="Vertical" Margin="2">
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </StackPanel>
        </Border>
    </DataTemplate>