views:

130

answers:

1

The following displays an image correctly in a silverlight usercontrol:

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

However, I want to dynamically bind the image to XAML, e.g. like this:

#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:

<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?

+2  A: 

The Image.Source property is of type ImageSource, so your ViewModel should expose an ImageSource. Image is a control, it has nothing to do in the 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 :

<ContentControl Content="{Binding MainImage}" />
Thomas Levesque
But this is a silverlight app and the DataManager has already loaded all the images the app needs to use, so I want to just pull the Image object out of the DataManager collection and hook it up to the XAML, I don't want to pass a datasource and then have XAML continually loading it, I just want it to display the Image object that happens to be contained in the MainImage variable, is there not a way to do that, or do I need to have a Refresh() method in my code-behind and manually reattach the Image object to a ContentControl each time the image changes?
Edward Tanguay
I'm not sure I understand your question... Did you see the last part of my answer (perhaps you posted your comment before I edited it) ? Binding the content of a ContentControl should work. If it doesn't, try that : `<Image Source="{Binding MainImage.Source}"`
Thomas Levesque
sorry, our comments criss-crossed, yes, that works perfectly, exactly what I was looking for
Edward Tanguay