【问题标题】:Change Image Source when the Parent Button change its enabled state当父按钮更改其启用状态时更改图像源
【发布时间】:2013-04-23 11:25:37
【问题描述】:

我有这个 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 代码不起作用。错误在哪里?我该如何解决?

【问题讨论】:

    标签: c# wpf image xaml binding


    【解决方案1】:

    您的Trigger 适用于Image,而不是按钮,因此在这种情况下监控的IsEnabled 属性是Image.IsEnabled 属性,这不是您想要的。

    您可以为Button 创建一个Style 并在该Style 中使用Trigger 来根据IsEnabled 状态更改按钮的Content,或者您可以创建一个@ 987654332@ 与Image 作为Button 的内容,并根据按钮的启用状态更改ImageSource

    This Answer 似乎提供了您所要求的细节。

    【讨论】:

      【解决方案2】:

      图像源属性应采用以下格式:

      "/«YourAssemblyName»;component/«YourPath»/«YourImage.png»"

      例子

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

      查看这篇文章了解更多信息:

      WPF image resources

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-23
        • 2013-11-28
        • 2012-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多