Gary DeReese

In my code, I have a WPF Image control bound to a property of an entity class. The binding is configured so that source updates are explicit:

<Image Source="{Binding Path=ImageData, UpdateSourceTrigger=Explicit}" ... />

The application allows the user to change the image displayed by displaying a file dialog, then setting the Image.Source property to the selected image file like this:

this.imageControl.Source = new BitmapImage(new Uri(selectedFileName));

When the user selects to save the current info, the code calls the following to explicitly update the binding source of the Image control:

this.imageControl.GetBindingExpression(Image.SourceProperty).UpdateSource();

an exception is thrown because the GetBindingExpression() call returns null. It appears that even though the binding is defined in the XAML, the binding is cleared if the Image.Source is updated. Are there any gotchas that I'm missing Any help is appreciated.


Re: Windows Presentation Foundation (WPF) Binding Lost after changing Image.Source

lee d

instead of setting the image like this

this.imageControl.Source = new BitmapImage(new Uri(selectedFileName));
get the binding and update the binding. if you directly set the source, the binding you created will be lost






Re: Windows Presentation Foundation (WPF) Binding Lost after changing Image.Source

Gary DeReese

Do you mean that I should instead update the value in the property that is bound to this Image control I only wish to update the entity property through an explicit call to UpdateSource() so that any actions on the form can be undone if the user cancels the operation.




Re: Windows Presentation Foundation (WPF) Binding Lost after changing Image.Source

lee d

can you just get the bindingexpression and do something like this

ImageData="new image";

BindingExpression be = img.GetBindingExpression(Image.SourceProperty);

be.UpdateTarget();






Re: Windows Presentation Foundation (WPF) Binding Lost after changing Image.Source

Gary DeReese

If I'm not mistaken, that involves updating the ImageData property on my class, and propagating that change to the Image control using the UpdateTarget() call. This functions, but I want to update the image displayed in the control, but not update the bound property until an explicit call is made ( UpdateSource() ).

This is the same as the idea behind updating a TextBox value bound to a property with UpdateSourceTrigger=Explicit, which works as expected.

Is it clear what I am trying to accomplish Let me know if you need more info.




Re: Windows Presentation Foundation (WPF) Binding Lost after changing Image.Source

Sam Bent - MSFT

UpdateSource only makes sense for two-way bindings. Is your binding marked as two-way [Some properties, like TextBox.Text, are marked in metadata so that bindings are two-way by default, but Image.Source is not one of these. You have to mark the binding explicitly.]

This will do what I think you want, provided that your entity's ImageData property has the same type as the Image.Source property (or if there is a type converter that will do the conversion).

If the binding is only one-way, then the assignment this.imageControl.Source = new BitmapImage(new Uri(selectedFileName)); will override (i.e. discard) the binding.






Re: Windows Presentation Foundation (WPF) Binding Lost after changing Image.Source

Gary DeReese

Explicitly specifying the binding mode as TwoWay resolved my issue and everything is working fine. Thanks so much for the info!