2013-04-29 45 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