繁体   English   中英

WPF图像控制:一旦在代码中修改了源,触发器就会停止工作

[英]WPF Image Control: Trigger stops working once Source is modified in code

我在更改WPF图像控件的“Source”属性时遇到了一些问题。

我定义了三个图像源:

<Window.Resources>
    <BitmapImage x:Key="eyeSelImage" UriSource="/Images/eye-Sel.png" />
    <BitmapImage x:Key="eyeSelHlImage" UriSource="/Images/eye-SelHl.png" />
    <BitmapImage x:Key="eyeDisabled" UriSource="/Images/eye-Disabled.png" />
</Window.Resources>

我的图像定义如下所示:

<Image x:Name="testImage" Width="100" Height="100">
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Source" Value="{StaticResource eyeSelImage}"/>
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Source" Value="{StaticResource eyeDisabled}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

它按预期工作。 如果禁用图像控制,则图像会发生变化。 一旦启用,它就会变回。 我通过创建一个按钮(btn_DisableEnable)来测试它,当单击它时,切换'testImage'的'IsEnabled'属性。

但是,只要我在代码中更改'testImage'图像控件的'Source','IsEnabled'触发器就会停止工作。 我做了另一个按钮,并在其'Click'事件处理程序中执行以下操作:

BitmapImage tempImage = new BitmapImage();
tempImage.BeginInit();
tempImage.UriSource = new Uri("pack://application:,,,/testApp1;component/Images/eye-SelHl.png");
tempImage.EndInit();
testImage.Source = tempImage;

按下此按钮后,图像的源将正确更改为“eyeSelHlImage”资源。 但是,当单击“btn_DisableEnable”时,图像不再更改为其禁用的表示形式并返回。

可能是什么问题呢? 非常感谢所有帮助。

谢谢!

由于依赖属性可以通过不同的机制(触发器,样式,主题,继承等)设置,因此它已被定义为“设置”优先级列表。

你可以在这里找到它。

正如您可以看到本地值 - 即,如果您使用代码或XAML设置直接属性,或使用SetValue方法 - 具有更高的优先级而不是样式触发器

因此,按代码设置Source属性优先于触发器可以设置的值。

这就是为什么在您调用代码后,您的触发器不再起作用的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM