WPF控件不引发鼠标事件时该怎么办?

时间:2012-10-03 20:51:39

标签: wpf wpf-controls

我正在为程序编写自定义控件。该控件在其模板中有一个ComboBox等。像这样简单的东西可以重现:

 <Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ComboBox />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果我右键单击此控件,则会引发PreviewMouseRightButtonUp事件,但不会引发MouseRightButtonUp事件。我认为这是因为ComboBox控件在其自己的PreviewMouseRightButtonUp事件中将e.Handled设置为true,从而阻止引发其他事件。我正在猜测,因为如果我为ComboBox交换一个Rectangle,我会得到两个事件,如果在我的自定义控件中我在预览事件中将e.Handled设置为true,我只会得到预览事件。

我正在编写控件的程序期望在发生右键单击时引发两个事件,我怀疑我是否能让他们改变它。我是否必须以不吃预览事件的方式重新创建ComboBox,还是有另一种技巧?

1 个答案:

答案 0 :(得分:0)

您实际上可以在UIElement.Addhandler调用中接收传递为true的已处理事件。如果处理完毕,您可以举办新活动吗?

AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(OnMouseLeftButtonDow), true);

您可以使用snoop查看是否以及是否这样设置e.Handled = true;