Si...">

WPI UriSource Image, Data Binding and Caching

I have this currently for a database bound WPF image:

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

Simple enough.

Now adding caching to this image (I want to be able to manipulate / delete the local file after downloading it). I found that you can add CacheOption = "OnLoad" to the tag inside.

 <Image> <Image.Source> <BitmapImage UriSource="{Binding Path=ThumbFile, Converter={StaticResource myConverter2}}" /> </Image.Source> </Image> 

Then I had to convert to translate the local file to BitmapImage.

 <local:LocalUriToImageConverter x:Key="myConverter2"/> 

and

 public class LocalUriToImageConverter : System.Windows.Data.IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) { return null; } if (value is string) { value = new Uri((string)value); } if (value is Uri) { System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage(); bi.BeginInit(); //bi.DecodePixelWidth = 80; bi.DecodePixelHeight = 60; bi.UriSource = (Uri)value; bi.EndInit(); return bi; } return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new Exception("The method or operation is not implemented."); } } 

For some reason this doesn't even start working. There are no errors, but the controls do not seem to be connected. Breakpoints for both the ThumbFile property and the converter are not reached, although the control has many instances. Returning to another Source Source tag works just fine.

+4
source share
2 answers

I cannot tell from your code what is happening, but I would use Snoop to deploy it and see what is going on. You should be able to see any binding errors and see that the DataContext is in Image , and make sure that the ThumbFile property in your DataContext has what you expect.

+1
source

I had the same problem and I never found a way to make the binding work with any converter when using BitmapImage in the UriSource property. I assumed that this does not mean that you need to use this method.

However, I believe that the following code is equivalent and should work in your case (worked on mine):

 <Image Source="{Binding ThumbFile, Converter={StaticResource myConverter2}}" /> 
0
source

Source: https://habr.com/ru/post/1380653/


All Articles