2013-03-02 18 views
2

我遇到了一個問題,其中路徑內容的按鈕只檢測鼠標點擊的路徑。我希望爲ux在按鈕的任何位置註冊點擊。我已將按鈕的背景設置爲null和透明,所以頂部控件容器指定背景樣式。如何擊中無效或透明背景上的檢測?

下面是另一個SO後:Mouse event on transparent background

如前所述我都試過透明和空爲止。

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:wpfMyCustomControl"> 

<ControlTemplate x:Key="IconTemplate" TargetType="{x:Type ContentControl}"> 
    <Grid> 
     <Path Name="ForegroundSymbol" Data="M0,0 L1,0 1,1 0,1 0.5,0.5 z" Fill="{TemplateBinding Foreground}" Stretch="Fill" /> 
    </Grid> 
</ControlTemplate> 

<Style x:Key="IconButtonStyle" TargetType="{x:Type RepeatButton}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type RepeatButton}"> 
       <Grid> 
        <ContentControl Name="icon" Template="{StaticResource IconTemplate}" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 


<ControlTemplate x:Key="MyCustomTemplate" TargetType="{x:Type local:MyCustomControl}"> 
    <Grid Name="LayoutRoot" Background="Red"> 
     <RepeatButton Background="{x:Null}" Style="{StaticResource ResourceKey=IconButtonStyle}" /> 
    </Grid> 
</ControlTemplate> 

<Style TargetType="{x:Type local:MyCustomControl}"> 
    <Setter Property="Template" Value="{StaticResource ResourceKey=MyCustomTemplate}" /> 
</Style> 
</ResourceDictionary> 

如果我從「樣式」中刪除「x:Key」屬性,控件呈現。我已經能夠重現這個問題,使用上面的xaml控件樣式,點擊檢測不會在按鈕的「背景」部分觸發。

+0

如果你提供了一些代碼給我們看,會有很大的幫助。 – JerKimball 2013-03-02 00:51:55

+0

'iconstyle'如何?任何問題都可能與控制模板的「VisualTree」一致。 – JerKimball 2013-03-02 02:56:12

+0

我無法用按鈕,您的''和按鈕的{{:Null}'背景來重現此操作。這證明這是你造成這種情況的'Style'。開始解構風格,直到問題消失,或者以其他方式開始:從空白樣式開始,添加東西,直到問題出現。無論哪種方式,你會發現究竟是什麼導致問題,並反過來將使您能夠解決問題。 – 2013-03-04 07:12:58

回答

1

如果背景是透明的,如果您真的使用它,HitTesting將會起作用。您正在應用樣式,但沒有包含樣式,所以我不能說,但如果在該樣式中設置了模板,並且您沒有使用ControlTemplate中的背景,則設置背景無意義。通常,模板中的根元素的背景會對背景(通常是面板或邊框)執行TemplateBinding。元素本身不使用背景。另一種選擇是重寫HitTestCore方法。您可以在TextBlock的實現中查看Reflector/ILSpy,因爲它會覆蓋此實例以確保其rect中的任何點(而不僅僅是文本的字符)都是有效的生命點。

編輯:根據您提供的樣式/模板,我所描述的問題就是這個問題。您需要在ControlTemplate內使用背景,否則它不會產生任何影響。所以IconTemplate應該是這樣的:

<ControlTemplate x:Key="IconTemplate" TargetType="{x:Type ContentControl}"> 
    <Grid Background="{TemplateBinding Background}"> 
     <Path Name="ForegroundSymbol" Data="M0,0 L1,0 1,1 0,1 0.5,0.5 z" Fill="{TemplateBinding Foreground}" Stretch="Fill" /> 
    </Grid> 
</ControlTemplate> 
+0

你們把我帶到別的地方,這顯然是問題所在。在我的「