2013-04-29 50 views
1

我有這樣的XAML代碼:更改圖片來源時,母按鈕改變它的啓用狀態

<Button x:Name="ButtonUpdate" IsEnabled="False"> 
    <Image Source="Images/update gray.png" Name="ImgUpdate" > 
     <Image.Style> 
      <Style TargetType="{x:Type Image}"> 
       <Style.Triggers> 
        <Trigger Property="IsEnabled" Value="True"> 
         <Setter Property="Source" Value="Images/update.png" /> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </Image.Style> 
    </Image> 
</Button> 

我想,當ButtonUpdate啓用:

ButtonUpdate.IsEnabled = true; 

ImgUpdate.Source成爲"Images/update.png",當ButtonUpdate未啓用:

ButtonUpdate.IsEnabled = false; 

ImgUpdate.Source使用數據綁定變成"Images/update gray.png"

我的XAML代碼不起作用。錯誤在哪裏?我該如何解決它?

回答

1

Trigger適用於Image,而不是按鈕,這樣在這種情況下監控的IsEnabled屬性是Image.IsEnabled屬性,這是不是你想要的。

您可以創建爲Button一個Style,並使用TriggerStyle改變按鍵的基礎上,IsEnabled狀態Content,或者你可以用Image創建ControlTemplateButton和變化的內容ImageSource基於按鈕的啓用狀態。

This Answer似乎提供了你所要求的細節。

0

圖像源屬性應該是在此格式:

「/«YourAssemblyName»;component/«YourPath»/«YourImage.png»」

<Image Source="/WPFApplication;component/Images/Start.png" /> 

看到這篇文章的更多信息:

WPF image resources

+0

但啓動時圖像「Images/update gray.png」顯示正確... – Nick 2013-04-29 17:36:41