- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个场景导致 WPF 数据绑定(bind)和 INotifyPropertyChanged
出现奇怪的行为。我想要数据绑定(bind)源的私有(private)成员来处理 INotifyPropertyChanged.PropertyChanged
事件。
这是源代码:
<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/
我在我的 LINQ-to-SQL 类中修改了我的数据源(通过旧的删除和拖回方法),并且惊讶地看到 INotifyPropertyChanging 和 INotifyPropertyChanged 接口
我喜欢对需要 INotifyPropertyChanged 接口(interface)的完整属性进行一些较少重复和浪费的编码,并进行自定义属性。 背景 今天,为了在窗口中使用具有动态更新值的 MVVM
INotifyPropertyChanged 的目的是什么。我知道每当更改属性时都会触发此事件,但是 View /用户界面如何知道触发了此事件: 这是实现 INotifyPropertyChang
我已经设置了一个属性并实现了 INotifyPropertyChanged 像这样... public event PropertyChangedEventHandler PropertyChange
我有一个类(我们称它为 MyContainerClass ),它是其他几个类的容器(我们称它们为 ClassA 到 ClassF )。 ClassA至ClassF继承相同的基类(我们称之为 MyBas
鉴于: public class MyClass : INotifyPropertyChanged { public List _TestFire = new List(); stri
首先,我想说下面的示例过于简单化。 假设您已绑定(bind) WPF 控件。 Window 绑定(bind)到实现 INotifyPro
我想将窗口中的 TextBox 绑定(bind)到作为 View 模型变量的类中包含的属性,并确保 INotifyPropertyChanged 的 PropertyChanged 事件从类传播到
所有使用 MVVM 的 Silverlight 示例都使用名为 IPropertyChanged 的接口(interface)。它背后的概念是什么,为什么我们需要在设置一些值时引发事件? 例如:-
我正在阅读最新的Prism 4发行版的源代码,并且对解决此问题感兴趣。 ViewModels有一个基类,它实现INotifyPropertyChanged和INotifyDataErrorInfo并提
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 5年前关闭。 Improve this questi
我的应用程序中有 2 个 ViewModel。第一个(FirstPageViewModel)负责在我的 View 中的文本框中显示的数据。另一个 ViewModel (NavigationViewMo
这是我目前实现INotifyPropertyChanged的方式- public class ViewModel : INotifyPropertyChanged { public Perso
我的问题似乎是“范围”,尽管我不确定这是正确的术语。我想在设置自定义对象中的属性时通知只读列表重新评估自身。我相信它根本不知道它的存在。也许有一个简单的方法可以解决这个问题,我想不出,但我正在画一个空
在 WPF 中,我们(至少)有两个线程:渲染线程和 UI 线程。当我针对某些属性更改引发事件 OnNotifyPropertyChanged 时,它会在 UI 线程上引发。需要将此信息分派(dispa
下面的代码基于此 post : 我的问题:在这个简单的示例中,我看不出我做错了什么来让 INotifyPropertyChanged 导致 textBox1 绑定(bind)自动反射(reflect)
INotifyPropertyChanged 在 View 模型中对于数据绑定(bind)到 View 显然非常有用。当我想要通知属性更改时,我是否也应该在我的应用程序的其他部分(例如业务层)中使用此
假设我有一个订单行对象集合... public class OrderLine { public decimal Qty { get; set; } public decimal Co
我在此链接中看到以下代码:An elegant way to implement INotifyPropertyChanged 我是表达式树的新手。谁能解释一下这段代码是如何简单工作的? 谢谢 pri
我在给定 Type 的字段和属性上循环,我想测试字段类型或属性 Type 是否实现了 INotifyPropertyChanged。 也许这听起来很奇怪,但我会解析字段/属性,例如字符串、整数和其他类
我是一名优秀的程序员,十分优秀!