[英]How do I bind an Image dynamically in XAML?
以下内容在Silverlight用户控件中正确显示图像 :
Image image = pagingManager.BaseApp.DatasourceManager.GetImage("helloWorld1");
ContentArea.Content = image;
...
<ContentControl x:Name="ContentArea"/>
但是,我想将图像动态绑定到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");
而我的XAML是这样,但结果是它什么也没显示:
<Image Source="{Binding MainImage}"/>
我需要在XAML中放入什么以使其显示ViewModelProperty中具有的图像对象?
Image.Source
属性的类型为ImageSource
,因此您的ViewModel应该公开一个ImageSource
。 Image
是一个控件,它在ViewModel中无关。
如果确实在ViewModel中公开了Image
控件(这绝对是个坏主意),则应该在ContentControl
中而不是Image
控件中显示它:
<ContentControl Content="{Binding MainImage}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.