当内容更改时,ContentControl MouseDoubleClick事件不起作用

时间:2017-04-18 13:58:18

标签: c# .net wpf visual-studio

我有一个像这样的DataTemplate:

    <!-- 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>

BorderHwnd1就像这样:

    <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>

我的问题是,删除并以编程方式将新子项分配给MultiViewBorder1后,MultiViewBorder1_MouseDoubleClick事件不起作用。最初,MultiViewBorder1.Child是一个图像(No_image_available.png)。在我的代码中,我删除了图像并分配了一个新的子项。这个新孩子是HwndHost

我的错误在哪里?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

孩子正在改变WPF Image控制....它能够通过视觉树提升/流动“路由”“鼠标”事件......到你有的“hwnd”在HwndHost内部,它正在处理原始WIN32鼠标消息....并且不会路由它们。

为了能够将HwndHost中的鼠标活动放入可视化树中,您可以使用一些不同的技术来对其进行路由。

  • 有一个重叠的“透明”窗口

  • 处理WIN32 / Hwnd中的“鼠标”消息,然后使用“delegates”来提升

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

WPF WIN32 hwndhost WM_MOUSEMOVE WM_MOUSEHOVER