inotifypropertychanged

Handling PropertyChanged in a type-safe way

狂风中的少年 提交于 2019-11-30 13:26:41
There have been plenty of articles about how to use reflection and LINQ to raise PropertyChanged events in a type-safe way, without using strings. But is there any way to consume PropertyChanged events in a type-safe manner? Currently, I'm doing this void model_PropertyChanged(object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case "Property1": ... case "Property2": ... .... } } Is there any way to avoid hard-coding strings in a switch statement to handle the different properties? Some similar LINQ- or reflection-based approach? Timwi Let’s declare a method that can turn a

INotifyPropertyChanged and static properties

依然范特西╮ 提交于 2019-11-30 11:12:28
I'm tying myself in knots over a simple problem. I have a class that implements INotifyPropertyChanged . Some of the instance properties' getters use static properties and thus their values may change if the static property changes? Here's a simplified example. class ExampleClass : INotifyPropertyChanged { private static int _MinimumLength = 5; public static int MinimumLength { get { return _MinimumLength; } set { if (_MinimumLength != value) { _MinimumLength = value; //WHAT GOES HERE } } } private int _length = -1; public int length { get { return (_length > _MinimumLength) ? _length :

Why is INotifyPropertyChanged not updating the variables in XAML?

北慕城南 提交于 2019-11-30 10:37:40
I want to simulate data changing in the model and having that data be reflected in XAML. As I understand I need to implement INotifyPropertyChanged. However, in the following code example, XAML displays the data from the customer only once but the date and time never changes. What do I have to change in the following example to make XAML continually display the current date and time as it changes in the model? In particular, I don't understand how the Binding would know when to check the model, every second? 10 times a second? There is no event that it is responding to. What am I missing here?

Create an event to watch for a change of variable

血红的双手。 提交于 2019-11-30 09:15:40
Let's just say that I have: public Boolean booleanValue; public bool someMethod(string value) { // Do some work in here. return booleanValue = true; } How can I create an event handler that fires up when the booleanValue has changed? Is it possible? Avoid using public fields as a rule in general. Try to keep them private as much as you can. Then, you can use a wrapper property firing your event. See the example: class Foo { Boolean _booleanValue; public bool BooleanValue { get { return _booleanValue; } set { _booleanValue = value; if (ValueChanged != null) ValueChanged(value); } } public event

WPF INotifyPropertyChanged for linked read-only properties

对着背影说爱祢 提交于 2019-11-30 09:05:54
I am trying to understand how to update the UI if I have a read-only property that is dependent on another property, so that changes to one property update both UI elements (in this case a textbox and a read-only textbox. For example: public class raz : INotifyPropertyChanged { int _foo; public int foo { get { return _foo; } set { _foo = value; onPropertyChanged(this, "foo"); } } public int bar { get { return foo*foo; } } public raz() { } public event PropertyChangedEventHandler PropertyChanged; private void onPropertyChanged(object sender, string propertyName) { if(this.PropertyChanged !=

How to exclude nonserializable observers from a [Serializable] INotifyPropertyChanged implementor?

微笑、不失礼 提交于 2019-11-30 08:44:59
I have almost a hundred of entity classes looking like that: [Serializable] public class SampleEntity : INotifyPropertyChanged { private string name; public string Name { get { return this.name; } set { this.name = value; FirePropertyChanged("Name"); } } [field:NonSerialized] public event PropertyChangedEventHandler PropertyChanged; private void FirePropertyChanged(string propertyName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } Notice the [field:NonSerialized] attribute on PropertyChanged . This is necessary as some of the

Binding works without INotifyPropertyChanged, why?

送分小仙女□ 提交于 2019-11-30 08:41:53
问题 This is how we do it normally: public class ViewModel : INotifyPropertyChanged { string _test; public string Test { get { return _test; } set { _test = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName] string property = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); } Now our property can be used by multiple elements in the view, e.g.: <TextBox Text="{Binding Test,

using a Code Snippet for INotifyPropertyChanged

对着背影说爱祢 提交于 2019-11-30 08:28:37
问题 I found this code snippet for INotifyPropertyChanged But it shows the code like this : I would have this : for public : capital letter for the first letter + ... for private : underscore + small letter for the first letter + ... How can I achieve this ? Edit : Without having to type the public and the private fields <Snippet> <Declarations> <Literal> <ID>type</ID> <ToolTip>Property type</ToolTip> <Default>string</Default> </Literal> <Literal> <ID>property</ID> <ToolTip>Property name</ToolTip>

Get Deleted Item in ItemChanging event of BindingList

≡放荡痞女 提交于 2019-11-30 08:16:28
I am using Binding List in my application along with ItemChanged event. Is there any way I could know the previous values of properties in ItemChanged event. Currently I am adding a separate property named 'OldValue' to achieve this. Is there any way to know about the deleted item in item changed event. I am not able to find any way to know which item has been deleted from the list. If I understand correctly, you want to get info about item which was deleted from binding list. I think the easiest way to do this will be creating your own binding list which derives from binding list. Inside you

Determining the caller inside a setter — or setting properties, silently

随声附和 提交于 2019-11-30 06:56:37
Given a standard view model implementation, when a property changes, is there any way to determine the originator of the change? In other words, in the following view model, I would like the "sender" argument of the "PropertyChanged" event to be the actual object that called the Prop1 setter: public class ViewModel : INotifyPropertyChanged { public double Prop1 { get { return _prop1; } set { if (_prop1 == value) return; _prop1 = value; // here, can I determine the sender? RaisePropertyChanged(propertyName: "Prop1", sender: this); } } private double _prop1; // TODO implement