我对WPF MVVM不熟悉,我在绑定新的BitmapImage
到Image.Source
控件时遇到了问题。
从文件路径绑定图像很容易,但如何在WPF中将新的Bitmap
或BitmapImage
绑定到Image
标签?
我的方案:
我有一个处理图像的方法(它隐藏在Model
文件夹中的Class
中):
static public BitmapImage Init(Bitmap bmp)
{
//Do stuff
return BitmapConversion.Bitmap2BitmapImage(newImage);
//BitmapConversion is my own class for converting images
}
在这个部分,我描述了我想要对我的位图进行的操作并返回
BitmapImage
。在ViewModel中,我有以下内容:打开图像和初始化函数,我想要:
namespace Test.ViewModels
{
class ViewModel
{
public string filepath { get; set; }
public ViewModel()
{
OpenFileDialog openPicture = new OpenFileDialog();
openPicture.Filter = "Image files|*.bmp;*.jpg;*.gif;*.png;*.tif|All files|*.*";
openPicture.FilterIndex = 1;
if (openPicture.ShowDialog() == true)
{
filepath = openPicture.FileName;
Model.Init(new Bitmap(filepath)); //init function from above
}
}
public string DisplayedImage
{
get { return filepath; }
}
}
我的 View 长这样:
<!-- row1 -->
<Image Source="{Binding DisplayedImage}" />
<!-- row2 -->
<Image Source ={Binding ?}" />
我的问题是,如何正确地将像BitmapImage这样的对象绑定到Image标签的源?感谢任何建议。