将动态创建的位图对象绑定到 WPF 中的图像
我有一个不是静态资源的位图对象。通常,通过 WPF 中的图像绑定,您可以绑定到字符串路径。但是,我有一个动态创建的位图对象,我想绑定到该对象。是否可以执行类似的操作:
<WrapPanel x:Name="imageWrapPanel" HorizontalAlignment="Center">
<Image Source="{Binding Material1}" Margin="10" />
<Image Source="/NightVision;component/Images/concrete_texture.tif" Margin="10" />
</WrapPanel>
在文件后面的代码中,我有一个公共访问器:
public Bitmap Material1 {
get
{
return new Bitmap(/* assume created somewhere else*/)
}
}
上面的内容显然不起作用,但是,有没有办法执行类似的操作?
I have a bitmap object that is not a static resource. Normally, with image binding in WPF you can bind to a string path. However, I have a dynamically created bitmap object that I would like to bind to. Is it possible to do something like:
<WrapPanel x:Name="imageWrapPanel" HorizontalAlignment="Center">
<Image Source="{Binding Material1}" Margin="10" />
<Image Source="/NightVision;component/Images/concrete_texture.tif" Margin="10" />
</WrapPanel>
And in the code behind file I have a public accessor:
public Bitmap Material1 {
get
{
return new Bitmap(/* assume created somewhere else*/)
}
}
The above is obviously not working however, is there a way to do something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您唯一需要做的就是将
Bitmap
转换为可在Image
控件中使用的ImageSource
。因此,在您的绑定中,您可以添加Converter
可以实现这一点。转换的实现可能可以在 的答案中找到这个问题。(如果您有机会直接使用
BitmapImage
(WPF) 而不是Bitmap
(WinForms),这可能是个好主意)The only thing you need to do is convert the
Bitmap
to anImageSource
which can be used in theImage
control. So in your binding you can add aConverter
which accomplishes that. The implementation of the conversion is likely to be found in the answers to this question.(If you have a chance to work directly with
BitmapImage
(WPF) instead ofBitmap
(WinForms) that might be quite a good idea)