Reputation: 4167
I currently write an image viewer control that encapsulates a WPF Image Control and further stuff (Controls for applying filters and changing views). Here's the relevant portion of control's source code:
public partial class ImageViewPort : UserControl, INotifyPropertyChanged
{
private BitmapSource _source;
public static readonly DependencyProperty ImageDescriptorSourceProperty =
DependencyProperty.Register("ImageDescriptorSource",
typeof(ImageDescriptor),
typeof(ImageViewPort),
new UIPropertyMetadata(ImageDescriptorSourceChanged));
public ImageDescriptor ImageDescriptorSource
{
get { return (ImageDescriptor)GetValue(ImageDescriptorSourceProperty); }
set { SetValue(ImageDescriptorSourceProperty, value); }
}
public BitmapSource Source //the image control binds to this beauty!
{
get { return _source; }
set { _source = value; OnPropertyChanged("Source"); }
}
public ImageViewPort() { InitializeComponent(); }
private static void ImageDescriptorSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ImageViewPort viewPort = (ImageViewPort)d;
if (viewPort != null)
{
viewPort.TransformImage();
}
}
private BitmapSource TransformImage()
{
//do something that sets the "Source" property to a BitmapSource
}
}
The XAML Code (only relevant parts) of the user control:
<UserControl x:Name="viewPort">
<Image Source="{Binding ElementName=viewPort,Path=Source}"/>
</UserControl>
And finally the usage:
<WPF:ImageViewPort ImageDescriptorSource="{Binding Path=CurrentImage}"/>
In my window, I basically iterate a Collection and, as I do so, throw PropertyChanged notification for the CurrentImage property. That works, the getter is called each time, so the binding seems to work.
What I would expect to happen now is that the PropertyChanged-Callback of my UserControl is fired, but no such thing happens (It never steps in there, I've tried using breakpoints). I've tried the same thing binding a primitive type (int), and that worked.
Do you see any flaw in my implementation? Why isn't the user control updating? Thanks a lot in advance for any help!
Cheers
Sebi
Upvotes: 0
Views: 1011
Reputation: 11051
Check the output ... do you get any binding warnings? Also are you setting a new value? WPF knows when you try to set a value that is already set and ignores it. I would suggest thange the Metadata type to FrameworkPropertyMetadata and supply a proper default value.
To give this "comment" more value: Adding "PresentationTraceSources.TraceLevel=High" on the binding gives alot more information about how the binding tries to get its value, which also helps alot, finding problems that are not errors for WPF.
<TextBox Text="{Binding MyText, PresentationTraceSources.TraceLevel=High}"/>
Upvotes: 1