Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF图像控制:一旦源代码在代码中被修改,触发器就会停止工作_C#_Wpf - Fatal编程技术网

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

C# WPF图像控制:一旦源代码在代码中被修改,触发器就会停止工作,c#,wpf,C#,Wpf,我在更改WPF图像控件的“Source”属性时遇到一些问题 我定义了三个图像源: <Window.Resources> <BitmapImage x:Key="eyeSelImage" UriSource="/Images/eye-Sel.png" /> <BitmapImage x:Key="eyeSelHlImage" UriSource="/Images/eye-SelHl.png" /> <BitmapImage x:Ke

我在更改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>
按下此按钮后,图像的源将正确更改为“eyeSelHlImage”资源。但是,单击“btn_DisableEnable”(btn_DisableEnable)时,图像不再更改为其禁用的表示形式并返回

有什么问题吗?非常感谢您的帮助


谢谢

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

你可以找到它

如您所见,局部值——即,如果您使用代码或XAML或使用该方法直接设置属性,则具有更高的优先级,而不是样式触发器

因此,按代码设置
Source
属性的优先级取决于触发器可能设置的值


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

我以前见过这种解释,但由于我看到的每一篇文章中你发布的链接似乎都被破坏了,我无法找到详细信息(如果我单击我看到的链接“此主题不再可用”)。不过,你添加了一条评论,我在其他帖子中都没有看到。也就是说,代码中的设置也是“局部值”。谢谢我现在唯一的选择是实现“IsEnabledChanged”事件并在那里操作映像吗?是的@EEALNT,您可以。无论如何,你也可以考虑使用(我建议这样)。
<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>
BitmapImage tempImage = new BitmapImage();
tempImage.BeginInit();
tempImage.UriSource = new Uri("pack://application:,,,/testApp1;component/Images/eye-SelHl.png");
tempImage.EndInit();
testImage.Source = tempImage;