none
Using a binding with a UserControl RRS feed

  • Question

  • I'm building my first UserControl and am having some trouble with Binding, here's the code:

    <UserControl x:Class="ToDoIt.TimePicker"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"/>
       <TextBlock x:Name="Test" />
    </UserControl>
    


    It's being used as such:

    <my:TimePicker Time="{Binding RemindTime}" />
    


    The TimePicker.cs file is:

    	public partial class TimePicker : UserControl, INotifyPropertyChanged {
    
    		public static DependencyProperty TimeProperty = DependencyProperty.Register(
    				   "Time", typeof(DateTime), typeof(TimePicker));
    
    		public DateTime Time {	
    			get { return (DateTime)GetValue(TimeProperty);	}
    			set { SetValue(TimeProperty, value); this.NotifyChange("Time");  }
    		}
    
    		public TimePicker() {
    			InitializeComponent();
    		}
    
    		public event PropertyChangedEventHandler PropertyChanged;
    
    		protected void NotifyChange(string PropName) {
    			if ( this.PropertyChanged != null )
    				this.PropertyChanged(this, new PropertyChangedEventArgs(PropName));
    		}
    	}


    How can I know when the Time property is initialized and/or changed?  I have tried this from another post, which does indeed tell me when the Time property is changed, but only when its changed manually.  For example if the DataContext for the underlying Binding is changed, I do not get a change event occur.

    DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(TimeProperty, typeof(TimePicker));
    if ( dpd != null ) {
    	dpd.AddValueChanged(this, delegate {
    // This gets called when the bound value is actually changed, but not when it changes due to the DataContext changing.
    	});
    }
    
    Sunday, July 19, 2009 4:22 PM

Answers

  • Hi,

    I have no idea about the DependencyPropertyDescriptor, but what I'd do in order to receive change notifications from a dependency property is to register a change handler in the property metadata when declaring the property, like

    public static DependencyProperty TimeProperty = DependencyProperty.Register(
       "Time", typeof(DateTime), typeof(TimePicker), new PropertyMetaData(OnTimeChanged))

    OnTimeChanged being a static method that has the DependencyPropertyChangedHandler signature.





    Wednesday, July 22, 2009 6:25 AM

All replies

  • WPF does not use your CLI properties to get/set the value - instead it directly uses GetValue/Setvalue, and this is why your property notify code never fires. To monitor property changes correctly, see this article :

    http://serialseb.blogspot.com/2007/08/wpf-tips-5-receive-notifications-for.html
    http://blog.voidnish.com
    Sunday, July 19, 2009 5:02 PM
    Moderator
  • I already tried this, see the last 8 lines of my original post.   Forget that I have a PropertyChangedEventHandler in there, I'm not using it or trying to use it.
    Monday, July 20, 2009 12:01 AM
  • How can I know when the Time property is initialized and/or changed?  I have tried this from another post, which does indeed tell me when the Time property is changed, but only when its changed manually.  For example if the DataContext for the underlying Binding is changed, I do not get a change event occur.

    DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(TimeProperty, typeof(TimePicker));
    if ( dpd != null ) {
    	dpd.AddValueChanged(this, delegate {
    // This gets called when the bound value is actually changed, but not when it changes due to the DataContext changing.
    	});
    }
    

    Monday, July 20, 2009 12:02 AM
  • Anyone have any ideas why the DependencyPropertyDescriptor is not notifying me when the underlying DataContext is changed (due to list selection change)?
    Wednesday, July 22, 2009 2:23 AM
  • Hi,

    I have no idea about the DependencyPropertyDescriptor, but what I'd do in order to receive change notifications from a dependency property is to register a change handler in the property metadata when declaring the property, like

    public static DependencyProperty TimeProperty = DependencyProperty.Register(
       "Time", typeof(DateTime), typeof(TimePicker), new PropertyMetaData(OnTimeChanged))

    OnTimeChanged being a static method that has the DependencyPropertyChangedHandler signature.





    Wednesday, July 22, 2009 6:25 AM
  • Thanks much, that works.  I don't know why people have said this method didn't work and recommended what Nishant said, that method didn't work.  Thanks much hbarck!
    Wednesday, July 22, 2009 4:33 PM