locked
C# Wpf app: does anyone know how to get the [NotifyPropertyChangedInvocator] statement to work? RRS feed

  • Question

  • this is what the code looks like 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.ComponentModel;

    namespace WpfApplication6
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window, INotifyPropertyChanged
        {
            private string text;

            public string Text
            {
                get { return text; }
                set
                {
                    text = value;
                    OnPropertyChanged("Text");
                }
            }

            public MainWindow()
            {
                Text = "Some Text";
                DataContext = this;
                InitializeComponent();
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Text = "Another Text";
            }

            public event PropertyChangedEventHandler PropertyChanged;

            [NotifyPropertyChangedInvocator]
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    and the [NotifyPropertyChangedInvocator] doesn't work. does anyone know if i have to use more using commands or what?

    Tuesday, November 12, 2013 11:21 AM

Answers

  • Hi Heerabanani,

    Separate the Model from the Mainwindow and Implement INotifyPropertyChanged in the Model.

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DataContext = new ViewModel();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                (this.DataContext as ViewModel).Text = "Bye WPF";
            }
        }
    
        public class ViewModel : INotifyPropertyChanged
        {
            private string text;
    
            public string Text
            {
                get { return text; }
                set
                {
                    text = value;
                    OnPropertyChanged("Text");
                }
            }
    
            public ViewModel()
            {
                this.Text = "Hi WPF";
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    I checked it. It works fine


    srithar

    Tuesday, November 12, 2013 12:26 PM
  • The NotifyPropertyChangedInvocator attribute is a Resharper feature: http://www.spikie.be/blog/post/2013/07/02/Making-Resharper-and-MVVM-Lights-ViewModelBase-play-nice.aspx

    You could simply remove it from your code in order for it to compile:

             public event PropertyChangedEventHandler PropertyChanged;
    
             protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
             {
                 PropertyChangedEventHandler handler = PropertyChanged;
                 if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
             }
    

    Tuesday, November 12, 2013 8:01 PM

All replies

  • you don't need that to implement INotifyPropertyChanged.  from what I can find that attributed is used by the resharper tool to refactor any properties decorated with it to support property change notification.

    andy

    Tuesday, November 12, 2013 12:25 PM
  • Hi Heerabanani,

    Separate the Model from the Mainwindow and Implement INotifyPropertyChanged in the Model.

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DataContext = new ViewModel();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                (this.DataContext as ViewModel).Text = "Bye WPF";
            }
        }
    
        public class ViewModel : INotifyPropertyChanged
        {
            private string text;
    
            public string Text
            {
                get { return text; }
                set
                {
                    text = value;
                    OnPropertyChanged("Text");
                }
            }
    
            public ViewModel()
            {
                this.Text = "Hi WPF";
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    I checked it. It works fine


    srithar

    Tuesday, November 12, 2013 12:26 PM
  • The NotifyPropertyChangedInvocator attribute is a Resharper feature: http://www.spikie.be/blog/post/2013/07/02/Making-Resharper-and-MVVM-Lights-ViewModelBase-play-nice.aspx

    You could simply remove it from your code in order for it to compile:

             public event PropertyChangedEventHandler PropertyChanged;
    
             protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
             {
                 PropertyChangedEventHandler handler = PropertyChanged;
                 if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
             }
    

    Tuesday, November 12, 2013 8:01 PM