My PropertyChanged event is getting set properly when I watch the variable, but somewhere in code it gets reset to null and I have no idea how to figure out where this is happening.
Here is come code:
public event PropertyChangedEventHandler PropertyChanged;
//this is in the NotifyTaskCompletion class, as used from the blog
// http://msdn.microsoft.com/en-us/magazine/dn605875.aspx
private async Task WatchTaskAsync(Task task)
{
try
{
await task; //After this task, PropertyChanged gets set to a non-null method("Void OnPropertyChanged()")
}
catch
{
}
//PropertyChanged, which was set to a value after the task was run, and still not null during IdentifyCompleted getting set, is now null here
var propertyChanged = PropertyChanged;
if (propertyChanged == null)
return;
//other code
}
//My Watch variable of PropertyChanged is still good after this setter is run.
public NotifyTaskCompletion<GraphicCollection> IdentifyCompleted
{
get { return _IdentifyCompleted; }
set
{
_IdentifyCompleted = value;
// _IdentifyCompleted.PropertyChanged+= new PropertyChangedEventHandler(this, new PropertyChangedEventArgs("IdentifyCompleted"));
// NotifyPropertyChanged();
}
}
My main issue is that I cannot use a {set;get;} on PropertyChanged to attempt to identify WHERE it is getting set to null. So my main question, unless anyone sees something that is obviously wrong, is how would I go about finding out where it is getting set to null? Thank you for any assistance.
EDIT
As per the last posters suggestion, I set my code as follows:
private PropertyChangedEventHandler _propertyChanged;
public event PropertyChangedEventHandler PropertyChanged
{
add { _propertyChanged += value; }
remove { _propertyChanged -= value; }
}
And here is the issue.
//this is in my View Model. The ViewModel CONTAINS NotifyTaskCompletion<GraphicCollection> IdentifyCompleted which in turn implements INotifyPropertyChanged and has its own PropertyChanged that is not getting set
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
//This line sets the PopertyChanged in the view model AND in the NotifyTaskCompletion class somehow, but I don't know how it is setting it properly in the NotifyTaskCompletion class in my other project where this code works, When I step through this line in my code, it doesn't trigger
//the add{} of the PropertyChanged in NotifyTaskCompletion, but it DOES in my other project...
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
So with all that said, I can now see what line SHOULD be working, but I have no idea WHY it isn't working. Any other ideas? Thanks for the help so far.
You can write your own event accessors:
private PropertyChangedEventHandler propertyChanged;
public event PropertyChangedEventHandler PropertyChanged {
add { propertyChanged += value; }
remove { propertyChanged -= value; }
}
You can then set breakpoints.
Note that this is not thread-safe.
Related
For building the WPF application I used VS2022 (V17.4.4) and NuGet Fody.PropertyChanged Package (V4.1.0).
If a property is set directly, the PropertyChanged event will be raised for the property itself and all the [DependsOn] properties as well.
But if only the underlying field of the property is set and the PropertyChanged event is raised later manually by call of RaisePropertyChanged(nameof(Property)) the [DependsOn] properties will not raise any PropertyChanged event.
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace FodyPropertyChanged
{
[PropertyChanged.AddINotifyPropertyChangedInterface]
public class MainWindowViewModel : INotifyPropertyChanged
{
private DeviceStates _DeviceState;
private event PropertyChangedEventHandler _propertyChanged;
public event PropertyChangedEventHandler PropertyChanged
{
add { _propertyChanged += value; }
remove
{
if (_propertyChanged != null)
_propertyChanged -= value;
}
}
public DeviceStates DeviceState
{
get { return _DeviceState; }
set { _DeviceState = value; }
}
[PropertyChanged.DependsOn(nameof(DeviceState))]
public bool IsBusy
{
get { return DeviceState == DeviceStates.Working; }
}
public void SetDeviceStateTo(DeviceStates deviceState)
{
DeviceState = deviceState;
}
public void SetDeviceStateToAndRaisePropertyChangedAfterwards(DeviceStates deviceState)
{
_DeviceState = deviceState;
RaisePropertyChanged(nameof(DeviceState));
}
private void RaisePropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChangedEventHandler handler = _propertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
If I call SetDeviceStateTo(...) the PropertyChanged event will be raised for DeviceState and IsBusy (GUI will be updated, correctly). But if I call SetDeviceStateToAndRaisePropertyChangedAfterwards(...) the PropertyChanged event will be raised for DeviceState, only (IsBusy bindings will not be updated).
Any idea?
Like canton7 and BionicCode said:
[DependsOn] is only working for the setter not for events. So it is a known limitation and not a bug.
What is the best way to bind a property to a control so that when the property value is changed, the control's bound property changes with it.
So if I have a property FirstName which I want to bind to a textbox's txtFirstName text value. So if I change FirstName to value "Stack" then the property txtFirstName.Text also changes to value "Stack".
I know this may sound a stupid question but I'll appreciate the help.
You must implement INotifyPropertyChanged And add binding to textbox.
I will provide C# code snippet. Hope it helps
class Sample : INotifyPropertyChanged
{
private string firstName;
public string FirstName
{
get { return firstName; }
set
{
firstName = value;
InvokePropertyChanged(new PropertyChangedEventArgs("FirstName"));
}
}
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, e);
}
#endregion
}
Usage :
Sample sourceObject = new Sample();
textbox.DataBindings.Add("Text",sourceObject,"FirstName");
sourceObject.FirstName = "Stack";
A simplified version of the accepted answer that does NOT require you to type names of properties manually in every property setter like OnPropertyChanged("some-property-name"). Instead you just call OnPropertyChanged() without parameters:
You need .NET 4.5 minimum.
CallerMemberName is in the System.Runtime.CompilerServices namespace
public class Sample : INotifyPropertyChanged
{
private string _propString;
private int _propInt;
//======================================
// Actual implementation
//======================================
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
//======================================
// END: actual implementation
//======================================
public string PropString
{
get { return _propString; }
set
{
// do not trigger change event if values are the same
if (Equals(value, _propString)) return;
_propString = value;
//===================
// Usage in the Source
//===================
OnPropertyChanged();
}
}
public int PropInt
{
get { return _propInt; }
set
{
// do not allow negative numbers, but always trigger a change event
_propInt = value < 0 ? 0 : value;
OnPropertyChanged();
}
}
}
Usage stays the same:
var source = new Sample();
textbox.DataBindings.Add("Text", source, "PropString");
source.PropString = "Some new string";
Hope this helps someone.
So I've got the following ComboBox with the SelectedValue bound to the Property below. With the following binding, when I set value, the binding/RaisePropertyChanged combination is throwing a StackOverflow Exception.
Here's the ComboBox
<ComboBox x:Name="WireType" ItemsSource="{x:Bind ViewModel.WireTypes}" SelectedValue="{x:Bind ViewModel.WireType, Mode=TwoWay}"/>
Here's the Property
public string WireType
{
get
{
return _wireType;
}
set
{
_wireType = value;
RaisePropertyChanged();
}
}
And here's the RaisePropertyChanged method.
private void RaisePropertyChanged([CallerMemberName] string caller = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(caller));
}
}
I'm pretty sure I've done this before. What am I missing?
My psychic powers suggest that the PropertyChanged event is trying to set the property value.
The setter should protect against the case where the value didn't change. ie-
set
{
if (_wireType != value) // or the appropriate comparison for your specific case
{
_wireType = value;
RaisePropertyChanged();
}
}
Of course a stack trace would confirm what's actually happening.
Try this
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string caller = "")
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(caller));
}
I've got quite simple problem but gives me a headache and I'm wasting too much time. I've ran out of ideas anyway:)
For some reason I can't pass the value of my property variable to my event handler. Here is what I've got , an for me everything is fine but it won't work:(
Any idea why it's not passing the actual value of the variable? Thanks in advance:)
Just as the name suggests, propertyName should contain the property's name, not value. Also see PropertyChangedEventHandler and PropertyChangedEventArgs on MSDN for more details.
As to why your event handler null, I suspect you haven't subscribed to it. You should have something like the following somewhere in your program:
obj.PropertyChanged += new PropertyChangedEventHandler(obj_PropertyChanged);
private void obj_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
....
}
then when obj.Moves changes obj_PropertyChanged will be called.
I understand your confusion, so let me give a little more explanations. Suppose you have two classes A and B, and you want B to be notified when a property of A is changed. Then you can make A implement INotifyPropertyChanged, and make B subscribe to the PropertyChanged event of A, like the following:
public class A: INotifyPropertyChanged
{
private int moves;
public int Moves
{
get { return moves; }
set
{
if (value != moves) {
moves = value;
OnPropertyChanged("Moves");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class B
{
private A a = new A();
public B()
{
a.PropertyChanged += new PropertyChangedEventHandler(a_PropertyChanged);
}
private void a_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
....
}
}
With the above, B.a_PropertyChanged will be called whenever A.Moves is changed. More precisely, if b is an instance of B, then b.a_PropertyChanged will be called whenever b.a.Moves is changed. Also please note how my implementation of the setter of Moves differs from yours.
I am trying to bind but it doesn't seem to be working :/
my code:
void Binding(velocity Object, Label Output, string Field)
{
Binding newBinding = new Binding();
newBinding.Source = Object;
newBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
newBinding.Path = new PropertyPath(Field);
Output.SetBinding(Label.ContentProperty, newBinding);
}
Binding(newProjectile.CurrentVelocity, lbl_CurrentVelOutput, "Magnitude"); // how i call it
Thanks a bunch!
edit: i dont get an errror, its just that on the output the label doesnt change.
edit: i have tried looking for how to implement the INotifyChange interface and got something like this
public class velocity : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler Handler = PropertyChanged;
if (Handler != null)
{
Handler(this, new PropertyChangedEventArgs(name));
}
}
public double Velocity
{
get { return Magnitude; }
set
{
Magnitude = value;
OnPropertyChanged("10");
}
}
but i have no idea what i am doing.
Your binding should work just fine, but if you want changes to that Magnitude property to automatically show up in your view, then you'll have to let WPF know about those changes. That's where the INotifyProperty interface comes in, as it allows your code to let WPF known which properties have been changed:
// In C#, the common convention is to give classes CamelCased names:
public class Velocity : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
// Local variables and method arguments are also camelCased,
// but they start with a lower-case character:
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
// Properties with default getters and setters automatically get a 'backing field',
// but we can't use that because we need to call OnPropertyChanged, so we'll have to
// manually write out things. Normally, you'd give the backing field a name similar
// to the property, so it's obvious that they belong together:
private double _magnitude;
public double Magnitude
{
get { return _magnitude; }
set
{
_magnitude = value;
// Here, you need to pass in the *name* of the property that's being changed,
// so WPF knows which views it needs to update (WPF can fetch the new value
// by itself):
OnPropertyChanged("Magnitude");
}
}
}