繁体   English   中英

如何在XAML中动态绑定图像?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM