2012-10-03 25 views
1

我正在爲程序編寫自定義控件。該控件在其模板中還有一個ComboBox。事情這樣簡單的作品重現:當WPF控件沒有引發鼠標事件時,我該怎麼辦?

<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,我只會看到預覽事件。

我正在編寫控制程序,期望在右鍵單擊時發生這兩個事件,我懷疑我能夠讓他們改變這一點。我將不得不以不採用預覽事件的方式重新創建組合框,還是有另一個竅門?

回答

0

您實際上可以在UIElement.Addhandler調用中接收處理的事件,並傳遞true。如果處理完畢,您可以舉辦新活動?

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

您可以使用snoop,看看如果是這樣,其元素將e.Handled = TRUE;

+0

這或多或少是我所做的;我實際上使用了EventManager.RegisterClassHandler(),但它做同樣的事情。 – OwenP

相關問題