locked
INotifyPropertyChanged with data bound control RRS feed

  • Question

  • If I have TextBox bound to Class property it works fine. If the same class implements INotifyPropertyChanged interface it will produce:

    A first chance exception of type 'System.NullReferenceException' occurred in System.Windows.dll
    A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

    as soon as it hits:

    if( PropertyChanged != null )

       PropertyChanged(this, new PropertyChangedEventArgs( "test" ) );

    Even if I don't subscribe to this event it already has two other listeners in the chain:

    System.Windows.Data.WeakPropertyChangedListener

    System.Windows.Data.WeakPropertyChangedListener

     

    Is this behavior correct?

     

     

    Wednesday, March 19, 2008 11:35 AM

Answers

  •  Unfortunately I don't have code anymore. Have to replace it with my own delegate which works fine. But in general as I remember it looks like:

    Tile.cs
    public class Tile : INotifyPropertyChanged
    {
        public double Width
        {
            set
            {
                if( PropertyChanged != null )
                    PropertyChanged( this, null );
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

    Pattern.cs
    public class Pattern
    {

        public Pattern()

        {

              Tile = new Tile();
              Tile.PropertyChanged += new PropertyChangedEventHandler( OnTilePropertyChanged );
        }

        void OnTilePropertyChanged( object sender, PropertyChangedEventArgs e )

        {

        }

        public Tile Tile { get; set; }
    }

    PatternProperties.xaml:
    <UserControl>
        <Canvas>
            <TextBox Height="20" x:Name="WidthCtrl" Width="50" Canvas.Left="70" Canvas.Top="85" FontSize="12" Text="{Binding Tile.Width, Mode=TwoWay}" AcceptsReturn="True"/>
        </Canvas>
    </UserControl>

    Page.xaml.cs
        private void ShowPatternProperties( UserControl properties )
        {
            properties.Visibility = Visibility.Visible;
            properties.DataContext = plan.Pattern; // property type of Pattern, see class above
        }

     

    So user changes width in UI control, which updates class object property and the last one notifies other class about that change.

    Wednesday, March 26, 2008 12:51 PM

All replies

  • Hello, can you show the code of your class?

    Friday, March 21, 2008 6:10 AM
  •  Unfortunately I don't have code anymore. Have to replace it with my own delegate which works fine. But in general as I remember it looks like:

    Tile.cs
    public class Tile : INotifyPropertyChanged
    {
        public double Width
        {
            set
            {
                if( PropertyChanged != null )
                    PropertyChanged( this, null );
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

    Pattern.cs
    public class Pattern
    {

        public Pattern()

        {

              Tile = new Tile();
              Tile.PropertyChanged += new PropertyChangedEventHandler( OnTilePropertyChanged );
        }

        void OnTilePropertyChanged( object sender, PropertyChangedEventArgs e )

        {

        }

        public Tile Tile { get; set; }
    }

    PatternProperties.xaml:
    <UserControl>
        <Canvas>
            <TextBox Height="20" x:Name="WidthCtrl" Width="50" Canvas.Left="70" Canvas.Top="85" FontSize="12" Text="{Binding Tile.Width, Mode=TwoWay}" AcceptsReturn="True"/>
        </Canvas>
    </UserControl>

    Page.xaml.cs
        private void ShowPatternProperties( UserControl properties )
        {
            properties.Visibility = Visibility.Visible;
            properties.DataContext = plan.Pattern; // property type of Pattern, see class above
        }

     

    So user changes width in UI control, which updates class object property and the last one notifies other class about that change.

    Wednesday, March 26, 2008 12:51 PM
  • Please refer to http://msdn2.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(VS.95).aspx for implementation of INotifyPropertyChanged Interface.

    Wednesday, March 26, 2008 9:42 PM
  •  I don't see anything wrong with my implementation.

    Wednesday, March 26, 2008 10:59 PM
  •  Hi, I am having the same problem with you. It looks like some wrong with the runtime. During debugging, I accidentally found the invocation lists are not equal even if they are supposed to. It's really hard to be believe what I see. I did two screen shots:

    http://picasaweb.google.com/lplusplus/TestAlbum/photo?authkey=Y9znz6sFgB4#5198512034169368274

    Any one has any idea or work around? 

    ---- Edit because I found my problem -----

    Ok, I finally found my problem: I raise the property changed event again in a property changed listener, and the enumerator seems doesn't like to be reentered, which makes sense to me. (The work around is to use begininvoke to serialize the access to the enumerator.)

    Friday, May 9, 2008 6:48 PM
  • Ok, I have to modify my post again because I finally located the problem with a test project available at:

    http://www.fileqube.com/shared/HlEIPFrG43499

    The solution file is in "sl-test-raiseEventError" after decompressing. There is also a Readme.txt file that describes the issue.

    Thanks

    --- edited with one small hint ---

    Be sure to turn NullReferenceException on in menu:Debug->Exceptions. For some reason, silverlight project will disable all exceptions, giving a illusion that everything is fine. 

    Wednesday, June 18, 2008 2:41 PM
  • Hi,

    I have the same problem, but I can't access the project in your last post, it seems to have disappeared...Can you post it again ?

    Thanks

    Robert.

    Tuesday, February 3, 2009 11:47 AM
  • I didn't use fileqube for a while and don't have the project showing the error right now. This is what I remembered:

    1. Doing data binding with DataGrid. At least two columns bound to different property.

    2. In one property changed event, modify the other property that is also bound to another column.

    You are most likely having the null exception in raising event changed code. The exception doesn't occur consistently, but should be very frequent.

    Wednesday, February 4, 2009 6:53 PM