INotifyPropertyChanged with data bound control
Last post 05-09-2008 6:48 PM by codism. 5 replies.
Sort Posts:
03-19-2008 11:35 AM
INotifyPropertyChanged with data bound control

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?

 

 

kgalenko

Joined on 03-04-2008
Posts 14
03-21-2008 6:10 AM
Re: INotifyPropertyChanged with data bound control

Hello, can you show the code of your class?

shanaolanxing - Please mark the posts as answers if they help and unmark if they don't.

Yi-Lun Luo - MSFT

Joined on 10-29-2007
Posts 1,084
03-26-2008 12:51 PM
Marked as Answer
Re: INotifyPropertyChanged with data bound control

 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.

kgalenko

Joined on 03-04-2008
Posts 14
03-26-2008 9:42 PM
Re: Re: INotifyPropertyChanged with data bound control

Please refer to http://msdn2.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(VS.95).aspx for implementation of INotifyPropertyChanged Interface.

chillys

Joined on 03-27-2008
Posts 1
03-26-2008 10:59 PM
Re: Re: INotifyPropertyChanged with data bound control

 I don't see anything wrong with my implementation.

kgalenko

Joined on 03-04-2008
Posts 14
05-09-2008 6:48 PM
Re: Re: INotifyPropertyChanged with data bound control

 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? 

codism

Joined on 03-21-2008
Posts 31