This project is read-only.

PropertyObserver : what am I doing wrong ?

Feb 15, 2010 at 5:57 PM
Edited Mar 2, 2010 at 8:20 AM

Hello, I can't get the PropertyObserver to work.

Window1.xaml :

<Window x:Class="TestPropertyObserver.Window1"
    Title="Window1" Height="300" Width="300">
            <TextBox Text="{Binding TextValue}"></TextBox>

Window1.xaml.cs :

public partial class Window1 : Window
        public Window1()

            ViewModel viewModel = new ViewModel();

            this.DataContext = viewModel;

            new PropertyObserver<ViewModel>(viewModel).RegisterHandler(x => x.TextValue,
                                                      model => Debug.WriteLine("In observer"));

ViewModel.cs :

public class ViewModel : ObservableObject
        private string textValue;

        public string TextValue
            get { return this.textValue; }
                this.textValue = value;
                Debug.WriteLine("In setter");

The text "In setter" is displayed but not the text "In observer"

Any ideas why ? Thanks

Edit: I got it: seems like I have to keep the a reference to the PropertyObserver instance for it to work

Mar 9, 2010 at 8:03 PM


Sorry the for the late reply, I've been very busy.  You nailed it with your EDIT on the original post.  Since PropertyObserver uses a weak event handler for the source object's PropertyChanged event, you must root the instance somewhere.  Otherwise the GC will collect the instance and your event handling method will never be invoked.