locked
update target when source changes WPF RRS feed

  • Question

  • I am trying to update the target when the source property changes by implementing the I notifypropertychanged interface.
    THis is the code
    public partial class Window1 : Window, INotifyPropertyChanged
           
        {
            private object[] _values;
                  private string bbb="test";
        
            public event PropertyChangedEventHandler PropertyChanged;
            public Window1()
            {
                InitializeComponent();      
              
            }        
            public string Display
            {
                get
                {
                    return bbb;
                }
                set
                {            
                    bbb=value;
                    OnPropertyChanged("Display");
                }

            }     
            protected void OnPropertyChanged(string name)
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                this.Display = "testing2";           
            }      
        }   

    In the XAML, I have a textblock with path set to the Display property.

    However, when the Onpropertychanged is called, the handler is always null.

    I also built the application shown in msdn for how to implement Inotifypropertychanged interface, but I have the same problem there.
    Any reason this could be?

    Friday, April 18, 2008 7:35 PM

Answers

  • I think that these are may be something wrong in your code. Here is my example which works well. Furthermore, I use Visual Studio 2008 RTM with .NET Framework 3.5 to do this.

     

    Code Snippet

    <Window

       x:Class="ForumProjects.MainWindow"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       x:Name="Window">

        <StackPanel>

            <TextBlock Text="{Binding Display, ElementName=Window}"/>

            <Button Click="OnTestButtonClick">Change Display Text</Button>

        </StackPanel>

    </Window>

    namespace ForumProjects

    {

        public partial class MainWindow : Window, INotifyPropertyChanged

        {

            public MainWindow()

            {

                InitializeComponent();

            }

     

            public event PropertyChangedEventHandler PropertyChanged;

            private void RaisePropertyChanged(string propertyName)

            {

                if (this.PropertyChanged != null)

                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

            }

     

            private string display = "Old Text";

            public string Display

            {

                get { return this.display; }

                set

                {

                    if (this.display != value)

                    {

                        this.display = value;

                        RaisePropertyChanged("Display");

                    }

                }

            }

     

            private void OnTestButtonClick(object sender, RoutedEventArgs e)

            {

                this.Display = "New text";

            }

        }

    }

     

     

    Best Regards,

    Wei Zhou

    Monday, April 21, 2008 6:37 AM

All replies

  • Normally you can do that by setting UpdateSourceTrigger=PropertyChanged of the binding, or is that not what you want?
    Or if you meant it the other way around, you also have to set Mode=OneWayToSource, which reverses the update direction.
    Friday, April 18, 2008 7:47 PM

  • I did try setting the Mode to OneWayToSource, but that does not work. When I click the button, and the Display property gets updated, I want the new value to show up in the textblock. I am trying to get the updated values to show in the UI textblock.

    I also tried the UpdateSourceTrigger, as well as textblock.getbindingexpression(dp).updatetarget - but this seems to work only when the object itself changes.
    Friday, April 18, 2008 8:01 PM
  • I think that these are may be something wrong in your code. Here is my example which works well. Furthermore, I use Visual Studio 2008 RTM with .NET Framework 3.5 to do this.

     

    Code Snippet

    <Window

       x:Class="ForumProjects.MainWindow"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       x:Name="Window">

        <StackPanel>

            <TextBlock Text="{Binding Display, ElementName=Window}"/>

            <Button Click="OnTestButtonClick">Change Display Text</Button>

        </StackPanel>

    </Window>

    namespace ForumProjects

    {

        public partial class MainWindow : Window, INotifyPropertyChanged

        {

            public MainWindow()

            {

                InitializeComponent();

            }

     

            public event PropertyChangedEventHandler PropertyChanged;

            private void RaisePropertyChanged(string propertyName)

            {

                if (this.PropertyChanged != null)

                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

            }

     

            private string display = "Old Text";

            public string Display

            {

                get { return this.display; }

                set

                {

                    if (this.display != value)

                    {

                        this.display = value;

                        RaisePropertyChanged("Display");

                    }

                }

            }

     

            private void OnTestButtonClick(object sender, RoutedEventArgs e)

            {

                this.Display = "New text";

            }

        }

    }

     

     

    Best Regards,

    Wei Zhou

    Monday, April 21, 2008 6:37 AM

  • Monday, April 21, 2008 3:36 PM