-1

I have a DataTemplate like this:

    <!-- MULTI VIEW TEMPLATE -->
    <DataTemplate x:Key="MultiViewTemplate" >
        <Grid x:Name="MultiViewGrid" ShowGridLines="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <ContentControl Content="{StaticResource BorderHwnd1}" Grid.Column="0" Grid.Row="0" x:Name="MultiViewBorder1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" MouseDoubleClick="MultiViewBorder1_MouseDoubleClick"/>
            <ContentControl Content="{StaticResource BorderHwnd2}" Grid.Column="1" Grid.Row="0" x:Name="MultiViewBorder2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" MouseDoubleClick="MultiViewBorder2_MouseDoubleClick"/>
            <ContentControl Content="{StaticResource BorderHwnd3}" Grid.Column="0" Grid.Row="1" x:Name="MultiViewBorder3" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" MouseDoubleClick="MultiViewBorder3_MouseDoubleClick"/>
            <ContentControl Content="{StaticResource BorderHwnd4}" Grid.Column="1" Grid.Row="1" x:Name="MultiViewBorder4" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" MouseDoubleClick="MultiViewBorder4_MouseDoubleClick"/>
        </Grid>
    </DataTemplate>

And the BorderHwnd1 is like this:

    <Border x:Key="BorderHwnd1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderBrush="Transparent" BorderThickness="0" Background="Transparent" >
        <Image Margin="1" Source="/MyProject;component/Images/No_image_available.png" />
    </Border>

My problem is that MultiViewBorder1_MouseDoubleClick event does not work after I remove and assign programmatically a new child to MultiViewBorder1. Initially MultiViewBorder1.Child is an image (No_image_available.png). In my code, I remove the image and assign a new child. This new child is a HwndHost.

Where is my mistake? any help is appreciated!

Jafuentes
  • 365
  • 2
  • 6
  • 13
  • 1. *"MultiViewBorder1_MouseDoubleClick event does not work"* -- exactly what do you mean, "does not work"? Exception? What? Please show us the handler and explain the symptoms. 2. *"after I remove and assign programmatically a new child to MultiViewBorder1"* -- that sentence could mean virtually anything. Show us the code. 3. You should be binding Content instead of setting it programmatically. – 15ee8f99-57ff-4f92-890c-b56153 Apr 18 '17 at 14:24
  • As a rule, it's a mistake to follow this form: *"Here's some code. It's fine. There's an error in some other code that I won't show you, but here's an incredibly vague description of how I felt about what I thought I was trying to accomplish when I wrote it. What's wrong?"* – 15ee8f99-57ff-4f92-890c-b56153 Apr 18 '17 at 14:27
  • MultiViewBorder1_MouseDoubleClick event does not work = MouseDoubleClick event not firing ` private void onMouseDoubleClick(object sender, RoutedEventArgs e) { MessageBox.Show("Double Click"); }` – Jafuentes Apr 18 '17 at 14:29

1 Answers1

1

The child is changing from a WPF Image control....which is able to raise/flow "routed" "mouse" events through the visual tree....to some "hwnd" which you have inside the HwndHost, which is handling raw WIN32 mouses messages....and doesn't route them.

To be able to get mouse activity in your HwndHost into the visual tree, you could use a few different techniques to get them routed.

  • have an overlayed "transparent" window

  • handle the "mouse" messages in your WIN32/Hwnd, and then use "delegates" to raise

How can I convert Win32 mouse messages to WPF mouse events?

WPF WIN32 hwndhost WM_MOUSEMOVE WM_MOUSEHOVER

Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47