2013-10-07 98 views
1

比方說,我有一個矩形這樣的:如何增加WPF矩形上的命中測試半徑?

<Rectangle Grid.Column="1" 
      Stroke="Red" 
      StrokeDashArray="4.0 4.0" 
      StrokeThickness="{Binding Path=CurrentThickness}" 
      Visibility="{Binding Path=VisibleRectangle, 
           Converter={StaticResource VisibilityConverter}}" 
      MouseUp="HandleMouseUp" /> 

這適用於在矩形本身MouseUp事件做命中測試。但是,矩形的典型寬度爲1px寬,難以點擊矩形的邊緣。我想讓「矩形」筆畫的「有效的單擊邊框大小」大於該筆畫的視覺外觀。 (例如,假設矩形繪製爲1px寬,但鼠標點擊區域實際上是3px寬)

這樣的事情是可能的,還是我被迫增加Rectangle中風的厚度?

回答

2

哈克解決方案:

把「透明」矩形在同一個地方,讓你的矩形IsHitTestVisible="False"

<Rectangle x:Name="Clickable" 
      Grid.Column="1" 
      MouseUp="HandleMouseUp" 
      Fill="#01FFFFFF"/> 

<Rectangle Grid.Column="1" 
      Stroke="Red" 
      StrokeDashArray="4.0 4.0" 
      StrokeThickness="{Binding Path=CurrentThickness}" 
      IsHitTestVisible="False" 
      Visibility="{Binding Path=VisibleRectangle, 
           Converter={StaticResource VisibilityConverter}}"/> 
+0

聽起來對我很好。 – Tico

+0

+1。只要可點擊的矩形繪製在頂部,您實際上不需要使顯示矩形命中測試不可見。 (例如Zorder或顛倒XAML中矩形的順序就足夠了) –

+0

老實說,我不認爲這是一個拙劣的解決方案。我們想要測試可見區域是一個矩形,所以我們可以使用矩形來實現這一點。勾上勾號。 (我使用Transparent而不是'#01FFFFFF') –

0

這是我最後使用,建立在HighCore的答案。請注意用於將顯示的矩形移動到命中測試矩形中間的邊距:

<!-- This border is displayed when XXX. --> 
<!-- Note that the margin moves it into the middle of the hit testing 
    rectangle below. --> 
<Rectangle Stroke="Red" 
      StrokeDashArray="4.0 4.0" 
      Margin="1" 
      StrokeThickness="{Binding Path=CurrentThickness}" 
      Visibility="{Binding Path=VisibleRectangle, 
           Converter={StaticResource VisibilityConverter}}" /> 

<!-- This border handles hit testing when XXX. --> 
<Rectangle Panel.ZIndex="10" 
      StrokeThickness="3" 
      Stroke="Transparent" 
      MouseUp="HandleMouseUp" 
      Visibility="{Binding Path=VisibleRectangle, 
           Converter={StaticResource VisibilityConverter}}" />