tags:

views:

37

answers:

2

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?

+1  A: 

By hard-coding the Source in the code, you are breaking the Binding in your XAML.

Instead of doing that, bind to a property that you set using (most of) the same code above. Here's one way to do that.

XAML:

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

C#:

    public ImageSource MovieImageSource
    {
        get { return mMovieImageSource; }
        // Set property sets the property and implements INotifyPropertyChanged
        set { SetProperty("MovieImageSource", ref mMovieImageSource, value); }
    }

   void SetMovieSource(string path)
   {
        myImage = new BitmapImage();
        myImage.BeginInit();
        myImage.UriSource = new Uri(path);
        myImage.EndInit();
        this.MovieImageSource = myImage;
   }
Wonko the Sane
+3  A: 

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)
{

}
Thomas Levesque
SetCurrentValue - nice - I wasn't aware of this in my rookie campaign into 4.0...
Wonko the Sane
So, I upgraded my project to 4.0 last night and used SetCurrentValue, but it still won't trigger the SourceUpdated event. I removed all the binding code in the XAML file and pointed the control's SourceUpdated event at the correct function. Am I still missing something?
Josh Deese
The "Source" word in the SourceUpdated event has nothing to do with the image source, it refers to the source of the binding. SourceUpdated will be triggered if the control updates the source of the binding, which will never happen with your code. But the TargetUpdated event should be triggered...
Thomas Levesque