0

I am having set of images and those paths are defined in a xaml file like below

<BitmapImage
        x:Key="LoginLogo"
        UriSource="pack://application:,,,/AssemblyName;component/Resources/LoginLogo.png" />

And in my view.xaml am having an image control and Source property is bound with View Model.

Property in view model :

private BitMapImage _imageSource
public BitmapImage ImageSource
        {
            get { return (_imageSource); }
            set { this.SetValue(_imageSource, value); }
        }

Now i need to set value to Image Source . How to set this.

5
  • 2
    You didn't try <Image Source="{Binding ImageSource}">? Besides that, what's the relation between your view model's ImageSource property and the XAML-defined BitmapImage resource? Commented Apr 28, 2017 at 8:59
  • @Clemens and changing ImageSource from BitmapImage to string Commented Apr 28, 2017 at 8:59
  • @MightyBadaboom For what reason? While you may argue if it's good practice, it is perfectly legal to have a VM property of type BitmapImage. Commented Apr 28, 2017 at 9:00
  • @Clemens Having the Image in your ViewModel would not be the cleanest way when following the MVVM pattern. Your ViewModel should not know how the View is using the uri of the image. Commented Apr 28, 2017 at 9:02
  • @MightyBadaboom That's plain wrong, besides that there is no knowledge of any URI in the VM. There is just a property of type BitmapImage, nothing more. The view model knows nothing about the view. Commented Apr 28, 2017 at 9:04

1 Answer 1

2

You would bind the Source property of an Image control to the view model property like this:

<Image Source="{Binding ImageSource}">

Setting the view model property to the BitmapImage resource would perhaps look like shown below (e.g. in the MainWindow class):

viewModel.ImageSource = (BitmapImage)Resources["LoginLogo"];

You may also consider to change the type of your view model property from BitmapImage to the base class ImageSource. This would provide greater flexibility in terms of the different image types you may assign to the property, e.g. DrawingImage or BitmapFrame etc.


An alternative would be a view model property of type Uri (or string) holding the image URL

private Uri imageUrl
public Uri ImageUrl
{
    get { return imageUrl; }
    set { SetValue(imageUrl, value); }
}

You would bind it the same way, like <Image Source="{Binding ImageUrl}"> (and thus benefit from built-in automatic type conversion), but could save the BitmapImage resource and directly assign a property value like this:

viewModel.ImageUrl =
    new Uri("pack://application:,,,/AssemblyName;component/Resources/LoginLogo.png");
Sign up to request clarification or add additional context in comments.

2 Comments

Beware of using Binding for images as WPF will lock the file for the (duration of execution) of the application. From my experience using a converter on the image source property and then returning a bitmap image is much safer.
There is no file involved. The question is about an assembly resource. For local files, a binding converter should set BitmapCacheOption.OnLoad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.