Binding an Image in Wpf Mvvm

Binding an Image in WPF MVVM

Displaying an Image in WPF is much easier than that. Try this:

<Image Source="{Binding DisplayedImagePath}" HorizontalAlignment="Left" 
Margin="0,0,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Bottom"
Grid.Row="8" Width="200" Grid.ColumnSpan="2" />

And the property can just be a string:

public string DisplayedImage 
{
get { return @"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"; }
}

Although you really should add your images to a folder named Images in the root of your project and set their Build Action to Resource in the Properties Window in Visual Studio... you could then access them using this format:

public string DisplayedImage 
{
get { return "/AssemblyName;component/Images/ImageName.jpg"; }
}

UPDATE >>>

As a final tip... if you ever have a problem with a control not working as expected, simply type 'WPF', the name of that control and then the word 'class' into a search engine. In this case, you would have typed 'WPF Image Class'. The top result will always be MSDN and if you click on the link, you'll find out all about that control and most pages have code examples as well.


UPDATE 2 >>>

If you followed the examples from the link to MSDN and it's not working, then your problem is not the Image control. Using the string property that I suggested, try this:

<StackPanel>
<Image Source="{Binding DisplayedImagePath}" />
<TextBlock Text="{Binding DisplayedImagePath}" />
</StackPanel>

If you can't see the file path in the TextBlock, then you probably haven't set your DataContext to the instance of your view model. If you can see the text, then the problem is with your file path.


UPDATE 3 >>>

In .NET 4, the above Image.Source values would work. However, Microsoft made some horrible changes in .NET 4.5 that broke many different things and so in .NET 4.5, you'd need to use the full pack path like this:

<Image Source="pack://application:,,,/AssemblyName;component/Images/image_to_use.png">

For further information on pack URIs, please see the Pack URIs in WPF page on Microsoft Docs.

How to bind the Image.Source in MVVM correctly?

It turned out that I have an extra unneeded characters in my ImagePaths as Kyle stated. And then, I needed to set my Image.Source from within my View.cs. At least, this is how it worked for me:

ViewModel Something like this:

if (Whatever)
{
StatusIconPath = "/Resources/SessionView/X-With-Green-Check-White.png";
ResultIconPath = "/Resources/SessionView/Y-White.png";
}

Then in View.cs and on SelectedItemChanged:

private void Grid_SelectedItemChanged(object sender, DevExpress.Xpf.Grid.SelectedItemChangedEventArgs e)
{
string tempStatus = ((SessionViewModel) DataContext).StatusIconPath;
string tempResult = ((SessionViewModel) DataContext).ResultIconPath;
StatusImage.Source = new BitmapImage(new Uri(@tempStatus, UriKind.Relative));
ResultImage.Source = new BitmapImage(new Uri(@tempResult, UriKind.Relative));
}

and in Xaml: just a fallback value(any original/default image we want). Ex:

<Image Name="ResultImage" Source="/EZ3D;component/Resources/SessionView/Retake-White.png"/>

Adding an image in WPF USING MVVM through BindableCollection

Your ImageTicket property declaration is wrong. It should return an ImageSource:

public ImageSource ImageTicket
{
get { return new BitmapImage(new Uri(ImageSource, UriKind.Relative)); }
}

The setter did not make any sense, because it did nothing but recursively calling itself.

The whole property may be redundant, since due to built-in automatic type conversion you could also directly bind to the ImageSource property:

<Image Source="{Binding ImageSource}"/>

But be aware that if you use a Resource File Pack URI in code behind, it should be fully prefixed:

ImageSource = "pack://application:,,,/Assets/Icons/man.png",

and the image file's Build Action should be set to Resource.

C#, binding new BitmapImage, WPF MVVM

This should work with ImageSource instead of string:

public ImageSource DisplayedImage
{
get { return new BitmapImage(new Uri(filepath)); }
}

Or with your ViewModel:

class ViewModel
{
public ViewModel()
{
...

if (openPicture.ShowDialog() == true)
{
DisplayedImage = Model.Init(new Bitmap(openPicture.FileName));
}
}

public ImageSource DisplayedImage { get; private set; }
}

You should however consider to replace your Model.Init method with one that operates on a WPF BitmapSource instead of a WinForms Bitmap.

Wpf binding image file to BitmapImage UriSource in MVVM way

Neither a BitmapImage resource nor a Binding Converter is needed.

This works out of the box, due to built-in type conversion from string to
ImageSource:

<Setter Property="Source" Value="{Binding OpenedFolderPath}"/>

In the ItemTemplate of the TreeView this Binding will not work, because it doesn't have the expected DataContext. You would write

<Setter Property="Source" 
Value="{Binding DataContext.OpenedFolderPath,
RelativeSource={RelativeSource AncestorType=TreeView}}"/>

Besides that, a converter for the UriSource property of a BitmapImage would have to return an Uri, not another BitmapImage:

public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
return new Uri(value.ToString());
}

Image is not showing up when binding the Source in WPF

C:/Image.png is not a relative URI, hence UriKind.Relative is wrong.

Try

LSImage = new BitmapImage(new Uri(settings.Path, UriKind.RelativeOrAbsolute));

As a note, setting NotifyOnSourceUpdated=True on the Image.Source Binding seems pointless, since you don't use the Binding's SourceUpdated event.

This is sufficient:

<Image Source="{Binding LSImage}"/>

WPF MVVM Binding Image Control to Image in Resources

In a typical WPF application you would not put images into Resources.resx and access them by the Resources class. Instead you would just add the image files to your Visual Studio project (perhaps in a folder called Images) and set their Build Action to Resource. Now you are able to access them by means of a Pack URI, and the Convert method of your converter might look like this:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
object result = null;

switch ((EstateCode)value)
{
case EstateCode.EstateCode1:
result = new BitmapImage(new Uri("pack://application:,,,/Images/Estate1.jpg"));
break;
case EstateCode.EstateCode2:
result = new BitmapImage(new Uri("pack://application:,,,/Images/Estate2.jpg"));
break;
}

return result;
}


Related Topics



Leave a reply



Submit