Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the SourceUpdated event trigger for my Image Control in WPF?

Tags:

c#

.net-3.5

wpf

I have an image control on a Window in my WPF project

XAML:

<Image 
  Source="{Binding NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" 
  Binding.SourceUpdated="bgMovie_SourceUpdated" 
  Binding.TargetUpdated="bgMovie_TargetUpdated" />

In code I am changing the source of the image

C#:

myImage = new BitmapImage();
myImage.BeginInit();
myImage.UriSource = new Uri(path);
myImage.EndInit();
this.bgMovie.Source = myImage;

But the bgMovie_SourceUpdated event is never triggered.

Can anyone shed some light on what I'm doing wrong?

like image 702
Josh Deese Avatar asked Sep 27 '10 18:09

Josh Deese


1 Answers

By assiging a value directly to the Source property, you're "unbinding" it... Your Image control is not databound anymore, it just has a local value.

In 4.0 you could use the SetCurrentValue method:

this.bgMovie.SetCurrentValue(Image.SourceProperty, myImage);

Unfortunately this method isn't available in 3.5, and there is no easy alternative...

Anyway, what are you trying to do exactly ? What is the point of binding the Source property if you're setting it manually anyway ? If you want to detect when the Source property changes, you can use the DependencyPropertyDescriptor.AddValueChanged method:

var prop = DependencyPropertyDescriptor.FromProperty(Image.SourceProperty, typeof(Image));
prop.AddValueChanged(this.bgMovie, SourceChangedHandler);
...

void SourceChangedHandler(object sender, EventArgs e)
{

}
like image 52
Thomas Levesque Avatar answered Oct 21 '22 01:10

Thomas Levesque