tags:

views:

52

answers:

3

How to disable all the properties or some of the properties PropertyChanged event for some time when we are using INotifypropertyChanged?

A: 

If you are referring to a binding, you can set the UpdateSourceTrigger to Explicit, which means any changes won't get saved until you explicitly tell it to update

<TextBox Text="{Binding SomeValue, UpdateSourceTrigger=Explicit}" />
Rachel
Thanks rachel... but i don't want to change the basic functionality of the UpdateSourceTrigger. see my UpdateSourceTrigger=PropertyChanged will be like this but i want to halt the updation for a while to set the some value, otherwise it will keep on calling the property. sometime that will be very time consuming depends on the criteria.
Kishore Kumar
A: 

In order for INotifyPropertyChanged to work, you need to raise the PropertyChanged event. Therefore, to make it not work, you just don't raise that event.

Here's a small example class:

public class NPCExample : INotifyPropertyChanged
{
    public NPCExample()
    {
    }

    private string mSomeProperty = "Set Property";
    public string SomeProperty
    {
        get { return mSomeProperty; }
        set
        {
            mSomeProperty = value;
            if (mUseNotifyPropertyChanged)
                NotifyPropertyChanged("SomeProperty");
        }
    }

    private Boolean mUseNotifyPropertyChanged = true;
    public Boolean UseNotifyPropertyChanged
    {
        get { return mUseNotifyPropertyChanged; }
        set
        {
            mUseNotifyPropertyChanged = value;
            NotifyPropertyChanged("UseNotifyPropertyChanged");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

In this class, each property calls the common "NotifyPropertyChanged" method for raising the PropertyChanged event. There is an additional variable defined (here, I used a public Property so I could bind it to a checkbox) that tells whether or not to raise the event, as used in the SomeProperty event.

Here's a small, quick-n-dirty program to show this in action: XAML

<Window x:Class="MyNamespace.SelectiveNotifyPropertyChanged"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SelectiveNotifyPropertyChanged" Height="300" Width="300">
    <StackPanel>
        <TextBlock Text="{Binding SomeProperty}" />
        <CheckBox x:Name="chkINPCEnabled"
                  Content="Enable INotifyPropertyChanged"
                  IsChecked="{Binding UseNotifyPropertyChanged}"></CheckBox>
        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="txtIsProperty"
                     Text="Set Property" />
            <Button x:Name="btnSetProperty"
                    Content="Set Property" />
        </StackPanel>
    </StackPanel>
</Window>

Code Behind

using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MyNamespace
{
    /// <summary>
    /// Interaction logic for SelectiveNotifyPropertyChanged.xaml
    /// </summary>
    public partial class SelectiveNotifyPropertyChanged : Window
    {

        public SelectiveNotifyPropertyChanged()
        {
            InitializeComponent();
            NPCExample example = new NPCExample();
            this.DataContext = example;

            btnSetProperty.Click += 
              (s, e) => example.SomeProperty = txtIsProperty.Text;
        }
    }

    public class NPCExample : INotifyPropertyChanged
    {
        public NPCExample()
        {
        }

        private string mSomeProperty = "Set Property";
        public string SomeProperty
        {
            get { return mSomeProperty; }
            set
            {
                mSomeProperty = value;
                if (mUseNotifyPropertyChanged)
                    NotifyPropertyChanged("SomeProperty");
            }
        }

        private Boolean mUseNotifyPropertyChanged = true;
        public Boolean UseNotifyPropertyChanged
        {
            get { return mUseNotifyPropertyChanged; }
            set
            {
                mUseNotifyPropertyChanged = value;
                NotifyPropertyChanged("UseNotifyPropertyChanged");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
Wonko the Sane
A: 

Based on your comment to Rachel it sounds like you might want to set the private property backing member sometimes. Could you expose a public method in your underlying class that would set the private member but not call NotifyPropertyChaged?

Public Class SomeClass

... define property SomeProp and m_SomeProp

 Public Sub SetSomeProp(val as string)
   m_SomePreop=val
 End Sub

End Class
Dave Lowther