我在更改WPF图像控件的“源”属性时遇到了一些问题。
我定义了三个图像源:
<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”时,图像不再更改为其禁用表示形式和返回。
可能的问题是什么?非常感谢所有的帮助。
谢谢!