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

Edw*_*uay 2 c# silverlight binding image mvvm

以下内容在silverlight用户控件中正确显示图像:

Image image = pagingManager.BaseApp.DatasourceManager.GetImage("helloWorld1");
ContentArea.Content = image;
...
<ContentControl x:Name="ContentArea"/>
Run Code Online (Sandbox Code Playgroud)

但是,我想动态地将图像绑定到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");
Run Code Online (Sandbox Code Playgroud)

而我的XAML就是这样,但结果是它没有显示任何内容:

<Image Source="{Binding MainImage}"/>
Run Code Online (Sandbox Code Playgroud)

我需要在XAML中放置什么才能显示我在ViewModelProperty中的图像对象?

Tho*_*que 5

Image.Source属性的类型的ImageSource,所以您的视图模型应该公开的ImageSource.Image是一个控件,它在ViewModel中无关.

如果你这样做暴露了Image你的视图模型控制(这绝对是一个坏主意),那么你应该在显示它ContentControl,而不是Image控制:

<ContentControl Content="{Binding MainImage}" />
Run Code Online (Sandbox Code Playgroud)