Tag: inotifypropertychanged

如何实现INotifyPropertyChanged

我需要帮助在我自己的数据结构类中实现INotifyPropertyChanged。 这是一个类赋值,但实现INotifyPropertyChanged是我在上面做的一个补充,超出了量规要求。 我有一个名为’BusinessRules’的类,它使用SortedDictionary来存储’Employee’类型的对象。 我有一个DataGridView显示我的所有员工,我想使用我的BusinessRules类对象作为我的DataGridView的DataSource。 分配需要BusinessRules容器。 我试图在这个类中实现INotifyPropertyChanged,但没有成功。 我工作的DataSource是一个BindingList。 目前,我使用BindingList作为’sidecar’容器并将其设置为我的DataSource。 我对BusinessRules类对象所做的每一个更改都会镜像到我的BindingList类对象。 但这显然是草率的编程,我想做得更好。 我试图在BusinessRules中实现INotifyPropertyChanged,但是当我将实例化的BusinessRules对象设置为DataSource时,DataGridView什么也没显示。 我怀疑问题是使用NotifyPropertyChanged()方法。 我不知道该传递什么,也不知道如何处理传入的内容。大多数示例都涉及更改名称,但是当将新对象添加到SortedDictionary时我更关心。 private void NotifyPropertyChanged( Employee emp ) { PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( emp.FirstName ) ); } 我需要更改什么才能使其正常工作? 你会解释为什么我的尝试不起作用吗? 在StackOverflow上形成我的问题,我出了名。 这不是故意的。 请告诉我您需要的其他信息,我会尽快提供。 这是我的BusinessRules源代码的链接 。

试图了解INotifyPropertyChanged

几个(新手)问题: 1)我看到了很多 public Person SelectedPerson { get; set; } 我假设这不会引发财产变更? 那么,如果我想这样做,我必须提供以下内容? private Person selectedPerson; public Person SelectedPerson { get { return this.selectedPerson; } set { if ((this.selectedPerson != value)) { this.selectedPerson = value; base.OnPropertyChanged(“SelectedPerson”); // Note: Using ViewModelBase } } } 2)如果我有以下内容: public bool CanDeletePerson { get { return SelectedPerson != null; } } 以及XAML […]

WPF中的INotifyPropertyChanged

尝试了解WPF。 这是我的测试类: public partial class MainWindow : Window, INotifyPropertyChanged { private ObservableCollection _myList = new ObservableCollection(); public ObservableCollection MyList { get { return _myList; } set { _myList = value; RaisePropertyChanged(“_myList”); } } public MainWindow() { InitializeComponent(); comboBox1.DataContext = _myList; } private void button1_Click(object sender, RoutedEventArgs e) { MyList = AnotherClass.SomeMethod(); } public event PropertyChangedEventHandler […]

是否可以将属性更改为属性?

我找到了Propperty Changed Event的一个实现,我可以在没有Web中属性名称的情况下更改Call Property。 然后我在这里建立了一个扩展方法 public static void OnPropertyChanged(this INotifyPropertyChanged iNotifyPropertyChanged, string propertyName = null) { if (propertyName == null) propertyName = new StackTrace().GetFrame(1).GetMethod().Name.Replace(“set_”, “”); FieldInfo field = iNotifyPropertyChanged.GetType().GetField(“PropertyChanged”, BindingFlags.Instance | BindingFlags.NonPublic); if (field == (FieldInfo) null) return; object obj = field.GetValue((object) iNotifyPropertyChanged); if (obj == null) return; obj.GetType().GetMethod(“Invoke”).Invoke(obj, new object[2] { (object) iNotifyPropertyChanged, (object) […]

使用Observable Collection MVVM绑定Pivot控件(Windows Phone 8)

我是WP8和MVVM的新手。 我创建了wp8应用程序,一旦用户登录就会请求各种数据。我只是不能让我的pivotots标头动态创建,我不知道是不是因为我在绑定中做了一些事情, INotifyPropertyChanged,两者或其他! 这是我到目前为止所做的: 我在App.cs中定义了一个全局MainViewModel,它将存储在登录时返回的所有数据。 一旦登录成功并且数据已加载到MainViewModel中,我将其重定向到包含数据透视控件的测试页面,并且我正在尝试动态创建数据透视表项。 这是我的测试页面的xaml,即MainPivotPage.xaml和我的MainPivotViewModel被初始化,因为它被定义为本地资源,并被设置为Pivot控件的datacontext,我不知道我是否正确这样做但我将“Name”属性分配给PivotItem的Header,它是存储在我的可观察集合Pivots中的对象。 属性Name是我在名为Pivot的类中包含的2个属性之一,它包含PivotId和Name。 <!—-> 创建MainPivotViewModel时,我将Pivots observable集合设置为存储在MainViewModel中的相同可观察集合,该集合包含登录时返回的所有数据。 正如您所看到的,我将它分配给属性而不是内部变量,以确保它将触发INotifyPropertyChanged(好吧……,我认为) public class MainPivotViewModel : BaseViewModel { private ObservableCollection _pivots = null; public MainPivotViewModel() { Pivots = App.MainViewModel.Pivots; } public ObservableCollection Pivots { get { return _pivots; } set { if (_pivots != value) this.SetProperty(ref this._pivots, value); } } } 我使用SetProperty函数,它包含在我的基类中,用于生成INotifyPropertyChanged事件,并允许我这样做,而无需在每次需要INotifyPropertyChanged事件时设置属性名称。 这是我的BaseView的代码: public class […]

WPF数据绑定标签内容

我正在尝试使用数据绑定创建一个简单的WPF应用程序。 代码看起来很好,但是当我更新我的属性时,我的视图没有更新。 这是我的XAML: 这是我的代码隐藏: namespace Calculator { public partial class MainWindow { public MainWindow() { DataContext = new CalculatorViewModel(); InitializeComponent(); } } } 这是我的视图模型 namespace Calculator { public class CalculatorViewModel : INotifyPropertyChanged { private String _calculatorOutput; private String CalculatorOutput { set { _calculatorOutput = value; NotifyPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void […]

属性或变量更改值时的Fire事件

我想为我所拥有的项目添加更多function,这些项目使用了.NET Framework中打包的许多类。 这些相同的类提供了许多属性,这些属性可以非常有用地适应我的项目的function,但是这些类缺少的一件事是事件。 如果每个属性都有一个适当的事件,只要这个属性的值发生变化就会触发,我可以根据这些属性值分配一个事件处理程序。 我制作了一个示例案例,以最简单的方式说明我的目标。 样例: System.Net.Sockets.Socket类( MSDN Docs上的Socket )有一个名为Connected的属性,如果套接字连接到指定的端点,则基本上返回true,否则返回false。 我想要完成的事情很简单。 我希望将此属性保留在“监视”下,当它的值发生变化时,触发一个事件。 这样做对我自己的一个类来说很简单,虽然使用INotifyPropertyChanged接口有点令人讨厌,只是因为我的代码总是改变了我必须手动触发事件的属性值。 不幸的是,据我所知,甚至这种程序都不能应用于.NET Framework中分布的现有Socket类。 好吧,这个问题变得非常广泛,抱歉,但我希望它能够深入了解我的目标。 现在简单地说,我想观察Socket类的Connected属性,当它的值发生变化时,触发一个事件。 如果有可能也使用这种方法来观察变量以及属性,那将是非常棒的,不仅对我而言,对于每个在SO上遇到这个问题的人来说都是如此。 当然,首选一种简单轻量级的方法,但最重要的是,我想了解它是如何完成的,所以将来我可以将它大规模地应用到其他类中。 我意识到我问了很多。 非常感谢。 有问题请问。

使用字符串常量更改通知属性

我正在使用一些现有代码,并试图找出在实现INotifyPropertyChanged接口时使用字符串常量作为属性名称的优势(如果有的话)。 例如,这样做: /* * Why use this instead of string literal * in OnPropertyChanged below?? */ public const string CustomerIdPropertyName = “CustomerId”; private int _customerId; public int CustomerId { get { return _customerId; } set { if (_cusomterId != value) { _customerId = value; OnPropertyChanged(CustomerIdPropertyName); } } } 而不是这个: private int _customerId; public int CustomerId […]

c#的ObservableDictionary

我正在尝试使用ObservableDictionary的以下实现: ObservableDictionary(C#) 。 当我将字典绑定到DataGrid时使用以下代码: ObserveableDictionary dd=new ObserveableDictionary(); …. dd[“aa”]=”bb”; …. dd[“aa”]=”cc”; at dd[“aa”]=”cc”; 我得到了以下exception Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 在以下方法中, CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, newItem, oldItem)中抛出此exception: private void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValuePair newItem, KeyValuePair oldItem) { OnPropertyChanged(); if (CollectionChanged != null) CollectionChanged(this, new […]

以类型安全的方式处理PropertyChanged

有很多文章关于如何使用reflection和LINQ以类型安全的方式引发PropertyChanged事件,而不使用字符串。 但有没有办法以类型安全的方式使用 PropertyChanged事件? 目前,我正在这样做 void model_PropertyChanged(object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case “Property1”: … case “Property2”: … …. } } 有没有办法避免在switch语句中硬编码字符串来处理不同的属性? 一些类似LINQ或基于reflection的方法?