2010-03-27 125 views
0

在下面的代碼中,MousePressImageButtonControl類的依賴項屬性。 以下綁定不起作用。感謝您解決此問題的幫助。WPF ControlTemplate和Binding

Value="{Binding RelativeSource={x:Static RelativeSource.Self}, 
       Path=MousePressImage}"/> 


<Style TargetType="{x:Type local:ButtonControl}"> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type local:ButtonControl}"> 
     <Border> 
      <Image x:Name="img" 
       Source="pack://application:,,,/Recipe_06_13;component/Resources/normal.bmp" 
       /> 
     </Border> 
     <!--<Border x:Name="border"> 
      <Border.Background> 
      <ImageBrush x:Name="img" 
         ImageSource="/Recipe_06_13;component/Resources/fatal.png"/> 
      </Border.Background> 
     </Border>--> 
     <ControlTemplate.Triggers> 
      <Trigger Property="IsPressed" Value="True"> 
      <Setter TargetName="img" 
        Property="Source" 
        Value="{Binding RelativeSource={x:Static RelativeSource.Self}, 
           Path=MousePressImage}"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style> 

我創建了這樣的ButtonControl

<local:ButtonControl Height="48" Width="160" 
    MouseOverImage="pack://application:,,,/Recipe_06_13;component/Resources/Over.bmp" 
    MousePressImage="pack://application:,,,/Recipe_06_13;component/Resources/Press.bmp" 
    DisableImage=" "> 
</local:ButtonControl> 
+0

什麼對象具有屬性「值」?請注意,這個Value屬性需要是一個依賴屬性才能使Binding工作。 MousePressImage實際上不必是依賴項屬性,儘管這簡化了屬性更改通知。 – 2010-03-27 20:34:16

+0

丹,感謝您的回覆..價值是屬性'源',我認爲源是一個依賴屬性...見下文。但是,綁定仍然不起作用...我錯過了什麼? <圖像X:名稱= 「IMG」 源= 「包://應用:,,,/Recipe_06_13;組件/資源/ normal.bmp」 /> codematrix 2010-03-27 20:39:58

回答

6

因爲你的觸發器是在控件模板,你需要從控件實例獲取MousePressImage被模板。要做到這一點,使用TemplateBinding或(更可靠)RelativeSource TemplatedParent

<Setter TargetName="img" 
     Property="Source" 
     Value="{Binding RelativeSource={RelativeSource TemplatedParent}, 
         Path=MousePressImage}" /> 
+0

IsMouseOver不應導致閃爍,因爲除鼠標移動狀態改變外,它不應該改變。您可能需要爲此編寫一個簡單的repro並將其作爲單獨的問題發佈 - 這可能會比評論中的討論獲得更多的關注和更好的答案!另外,我想你可能把我誤認爲別人 - 我從來沒有在Solyndra工作過 - 對不起! – itowlson 2010-03-27 20:59:44

+0

嘿,我看你已經做到了! – itowlson 2010-03-27 21:00:48