开发者

how to disable all or some properties PropertyChanged event in WPF?

How开发者_StackOverflow社区 to disable all the properties or some of the properties PropertyChanged event for some time when we are using INotifypropertyChanged?


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
    }
}


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}" />


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜