代码之家  ›  专栏  ›  技术社区  ›  sourcenouveau

WPF绑定失败,INotifyPropertyChanged.PropertyChanged的自定义添加和删除访问器失败

  •  1
  • sourcenouveau  · 技术社区  · 15 年前

    INotifyPropertyChanged . 我需要数据绑定源的私有成员来处理 INotifyPropertyChanged.PropertyChanged

    以下是源代码:

    XAML公司

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="TestApplication.MainWindow"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Height="100" Width="100">
        <StackPanel>
            <CheckBox IsChecked="{Binding Path=CheckboxIsChecked}" Content="A" />
            <CheckBox IsChecked="{Binding Path=CheckboxIsChecked}" Content="B" />
        </StackPanel>
    </Window>
    

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        public bool CheckboxIsChecked
        {
            get { return this.mCheckboxIsChecked; }
            set
            {
                this.mCheckboxIsChecked = value;
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null)
                    handler(this, new PropertyChangedEventArgs("CheckboxIsChecked"));
            }
        }
    
        private bool mCheckboxIsChecked = false;
    
        public MainWindow() { InitializeComponent(); }
    }
    

    期望的实现不起作用

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { lock (this.mHandler) { this.mHandler.PropertyChanged += value; } }
            remove { lock (this.mHandler) { this.mHandler.PropertyChanged -= value; } }
        }
    
        public bool CheckboxIsChecked
        {
            get { return this.mHandler.CheckboxIsChecked; }
            set { this.mHandler.CheckboxIsChecked = value; }
        }
    
        private HandlesPropertyChangeEvents mHandler = new HandlesPropertyChangeEvents();
    
        public MainWindow() { InitializeComponent(); }
    
        public class HandlesPropertyChangeEvents : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            public bool CheckboxIsChecked
            {
                get { return this.mCheckboxIsChecked; }
                set
                {
                    this.mCheckboxIsChecked = value;
                    PropertyChangedEventHandler handler = this.PropertyChanged;
                    if (handler != null)
                        handler(this, new PropertyChangedEventArgs("CheckboxIsChecked"));
                }
            }
    
            private bool mCheckboxIsChecked = false;
        }
    }
    
    2 回复  |  直到 15 年前
        1
  •  4
  •   Thomas Levesque    15 年前

    sender 传递给事件处理程序的参数是 HandlesPropertyChangeEvents ,当绑定需要 MainWindow .

    尝试更改您的代码,以便发件人是 主窗口 实例:

    private PropertyChangedEventHandler _propertyChanged;
    public event PropertyChangedEventHandler PropertyChanged
    {
        add { lock (this.mHandler) { this._propertyChanged += value; } }
        remove { lock (this.mHandler) { this._propertyChanged -= value; } }
    }
    
    
    ...
    
    public MainWindow()
    {
        InitializeComponent();
        mHandler.PropertyChanged += mHandler_PropertyChanged;
    }
    
    private void mHandler_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var handler = _propertyChanged;
        if (handler != null)
            _propertyChanged(this, e);
    }
    
        2
  •  2
  •   sourcenouveau    15 年前

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { lock (this.mHandler) { this.mHandler.PropertyChanged += value; } }
            remove { lock (this.mHandler) { this.mHandler.PropertyChanged -= value; } }
        }
    
        public bool CheckboxIsChecked
        {
            get { return this.mHandler.CheckboxIsChecked; }
            set { this.mHandler.CheckboxIsChecked = value; }
        }
    
        private HandlesPropertyChangeEvents mHandler = new HandlesPropertyChangeEvents();
    
        public MainWindow()
        {
            InitializeComponent();
            this.mHandler.Sender = this;
        }
    
        public class HandlesPropertyChangeEvents : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            public Sender { get; set; }
    
            public bool CheckboxIsChecked
            {
                get { return this.mCheckboxIsChecked; }
                set
                {
                    this.mCheckboxIsChecked = value;
                    PropertyChangedEventHandler handler = this.PropertyChanged;
                    if (handler != null)
                        handler(this.Sender, new PropertyChangedEventArgs("CheckboxIsChecked"));
                }
            }
    
            private bool mCheckboxIsChecked = false;
        }
    }
    

    这个例子有点做作,但在我的应用程序中,将事件处理代码移到绑定类之外是有意义的。