Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.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
如何使用Properties.Resources中的图像从WPF中的代码隐藏动态更改图像源?_Wpf_Imagesource - Fatal编程技术网

如何使用Properties.Resources中的图像从WPF中的代码隐藏动态更改图像源?

如何使用Properties.Resources中的图像从WPF中的代码隐藏动态更改图像源?,wpf,imagesource,Wpf,Imagesource,我有一个WPF应用程序,需要向用户提供有关内部状态的反馈。该设计有三个图像,分别称为红色、黄色和绿色。根据状态,每次将显示其中一个图像。以下是要点: 这三个图像位于代码隐藏中的Properties.Resources中 一次仅显示一个图像 状态更改来自代码隐藏中的进程,而不是用户 我想绑定一个图像控件,这样我就可以从代码隐藏中更改图像 我假设我需要一个图像转换器将JPG图像更改为图像源,例如: 我更喜欢在初始化期间转换一次图像,并保留图像源列表。我还假设需要一个dependency属性来

我有一个WPF应用程序,需要向用户提供有关内部状态的反馈。该设计有三个图像,分别称为红色、黄色和绿色。根据状态,每次将显示其中一个图像。以下是要点:

  • 这三个图像位于代码隐藏中的Properties.Resources中
  • 一次仅显示一个图像
  • 状态更改来自代码隐藏中的进程,而不是用户
  • 我想绑定一个图像控件,这样我就可以从代码隐藏中更改图像
我假设我需要一个图像转换器将JPG图像更改为图像源,例如:



我更喜欢在初始化期间转换一次图像,并保留图像源列表。我还假设需要一个dependency属性来绑定控件,但我不确定如何使用以下图像源列表设置该属性:



尽管WPF项目中的图像资源在
Resources.Designer.cs
中生成
System.Drawing.Bitmap
属性,但您可以直接从该资源创建图像。您只需将图像文件的设置为
资源
(而不是默认的

如果在Visual Studio项目的
Resources
文件夹中有一个文件
Red.jpg
,则创建
BitmapImage
如下所示。它使用一个

如果您在XAML中的某处声明了一个控件,如下所示:

<Image x:Name="image"/>
<Image Source="{Binding ImageUri}"/>

如果您希望通过绑定设置
属性,您可以创建一个
字符串
属性来返回图像URI。该字符串将由内置WPF自动转换为
BitmapImage

在XAML中,您可以像这样绑定到该属性:

<Image x:Name="image"/>
<Image Source="{Binding ImageUri}"/>
并以同样的方式绑定:

<Image Source="{Binding Image}"/>


更新:毕竟,您的图像不需要是VisualStudio项目中的资源。您只需添加一个项目文件夹,将图像文件放入该文件夹,并将其构建操作设置为
资源
。例如,如果调用文件夹
Images
,URI将是
pack://application:,,,/Images/Red.jpg

克莱门斯,你太棒了!这是一个奇妙的、全面的、易于理解的回答。我很想使用绑定选项,但在我的例子中,在代码隐藏中设置源代码的第一个选项太优雅了。唯一的问题是我无法让它与png文件一起工作。我希望有一个透明的背景。它应该与png文件一起工作吗?我只是做错了什么,还是它们不受支持?这应该也适用于PNG文件。也许您不应该将它们作为资源添加到项目中(请参见我的编辑)。只需使用您喜爱的图像工具创建文件,并将其添加到项目中的文件夹中即可。关于接受,克莱门斯,你又提供了一个清晰简洁的答案!当我简单地将PNG文件放在一个文件夹中并将其作为一个资源,而不是在资源设计器中将其作为properties.resource添加时,PNG文件工作得非常好。非常感谢。
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        ImageUri = "pack://application:,,,/Resources/Red.jpg";
    }

    public static readonly DependencyProperty ImageUriProperty =
        DependencyProperty.Register("ImageUri", typeof(string), typeof(MainWindow));

    public string ImageUri
    {
        get { return (string)GetValue(ImageUriProperty); }
        set { SetValue(ImageUriProperty, value); }
    }
}
<Image Source="{Binding ImageUri}"/>
public static readonly DependencyProperty ImageProperty =
    DependencyProperty.Register("Image", typeof(ImageSource), typeof(MainWindow));

public ImageSource Image
{
    get { return (ImageSource)GetValue(ImageProperty); }
    set { SetValue(ImageProperty, value); }
}
<Image Source="{Binding Image}"/>
private ImageSource imageRed =
    new BitmapImage(new Uri("pack://application:,,,/Resources/Red.jpg"));
private ImageSource imageBlue =
    new BitmapImage(new Uri("pack://application:,,,/Resources/Blue.jpg"));
...
Image = imageBlue;