gpt4 book ai didi

c# - WPF 绑定(bind)因 INotifyPropertyChanged.PropertyChanged 的​​自定义添加和删除访问器而失败

转载 作者:太空宇宙 更新时间:2023-11-03 19:34:12 25 4
gpt4 key购买 nike

我有一个场景导致 WPF 数据绑定(bind)和 INotifyPropertyChanged 出现奇怪的行为。我想要数据绑定(bind)源的私有(private)成员来处理 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;
}
}

最佳答案

这只是一个猜测,但我认为这可能是因为传递给事件处理程序的 sender 参数是 HandlesPropertyChangeEvents 的实例,而绑定(bind)需要一个实例主窗口

尝试更改您的代码,以便发件人是 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);
}

关于c# - WPF 绑定(bind)因 INotifyPropertyChanged.PropertyChanged 的​​自定义添加和删除访问器而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2952005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com