[英]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”时,图像不再更改为其禁用的表示形式并返回。
可能是什么问题呢? 非常感谢所有帮助。
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.