[英]How do I bind an Image dynamically in XAML?
The following displays an image correctly in a silverlight usercontrol: 以下内容在Silverlight用户控件中正确显示图像 :
Image image = pagingManager.BaseApp.DatasourceManager.GetImage("helloWorld1");
ContentArea.Content = image;
...
<ContentControl x:Name="ContentArea"/>
However, I want to dynamically bind the image to XAML , eg like this: 但是,我想将图像动态绑定到XAML ,例如:
#region ViewModelProperty: MainImage
private Image _mainImage;
public Image MainImage
{
get
{
return _mainImage;
}
set
{
_mainImage = value;
OnPropertyChanged("MainImage");
}
}
#endregion
...
MainImage = pagingManager.BaseApp.DatasourceManager.GetImage("helloWorld1");
And my XAML is this but the result is that it shows nothing : 而我的XAML是这样,但结果是它什么也没显示:
<Image Source="{Binding MainImage}"/>
What do I need to put in my XAML to make it display the image object I have in my ViewModelProperty? 我需要在XAML中放入什么以使其显示ViewModelProperty中具有的图像对象?
The Image.Source
property is of type ImageSource
, so your ViewModel should expose an ImageSource
. Image.Source
属性的类型为ImageSource
,因此您的ViewModel应该公开一个ImageSource
。 Image
is a control, it has nothing to do in the ViewModel. Image
是一个控件,它在ViewModel中无关。
If you do expose an Image
control in your ViewModel (which is definitely a bad idea), then you should display it in a ContentControl
, not an Image
control : 如果确实在ViewModel中公开了
Image
控件(这绝对是个坏主意),则应该在ContentControl
中而不是Image
控件中显示它:
<ContentControl Content="{Binding MainImage}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.