Manually invoking OnPropertyChanged for custom DependencyProperty - c#

So I've got a custom DependencyProperty like this:
public class PatientLookUp : TextBox
{
public static readonly DependencyProperty PatientProperty = DependencyProperty.Register("Patient", typeof(Patient), typeof(PatientLookUp), new FrameworkPropertyMetadata { BindsTwoWayByDefault = true });
public Patient Patient
{
get { return (Patient)GetValue(PatientProperty); }
set { SetValue(PatientProperty, value); }
}
//some logic for getting the Patient...
}
Edit: It is defined within a CustomControl. Another control using this CustomControl binds the Patient property and listens to it's changed event. The problem is, that I make some network request stuff and if I get a null Patient as a result, I still want to notify the other control that CustomControl has done it's stuff, even if the Patient hasn't really changed.
So now I want to invoke the PropertyChanged event manually. Is there any way to do this?
Hope you can help me, thanks.
Edit2: My XAML binding the Patient (shortened):
<Window Name="MyWindow">
<Grid>
<llc:PatientLookUp Patient="{Binding Patient}" MaxLength="10" Text="{Binding SocialSecurityNumber}" />
</Grid>
<Window>
Now I need to notify the ViewModel of MyWindow in case the Patient object of the PatientLookUp has changed. The LookUp has a request logic fired on pressing the enter key. After the request has finished, I want to notify MyWindow's ViewModel, even if it couldn't find anyone and the value is still null.

You're deriving from a standard WPF TextBox and putting something that is totally out of place.
You're approaching this in very inconvenient manner.
Instead: use a regular TextBox, bind to a PatientSearchViewModel.SearchString and when that SearchString is changed, fire the search (from the ViewModel) and if found, set the PatientSearchViewModel.Patient property and NotifyPropertyChange() so that the UI is notified of the change.
You're going like this:
DataModel -> View -> ViewModel
while the most straightforward and convenient approach is this:
DataModel -> ViewModel -> View

Your class needs to inherit from INotifyPropertyChanged interface and implement it's classes. Once that's done, you should be able to call OnPropertyChanged method on your property's set.
Inheriting INotifyPropertyChanged:
public partial class MainWindow : INotifyPropertyChanged
Implementation of INotifyPropertyChanged:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
Calling NotifyPropertyChanged:
public Patient Patient
{
get { return (Patient)GetValue(PatientProperty); }
set
{
SetValue(PatientProperty, value);
OnPropertyChanged();
}
}

Related

WPF MVVM Text Block Binding - Text is not visible [duplicate]

Can someone explain me why need to use implementation of INotifyPropertyChanged when using binding in wpf?
I can bind properties without implementation of this interface?
For example i have code
public class StudentData : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
string _firstName = null;
public string StudentFirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
OnPropertyChanged("StudentFirstName");
}
}
}
And binding in .xaml
<TextBox Text="{Binding Path=StudentFirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Grid.Row="1"
Grid.Column="2"
VerticalAlignment="Center" />
this code from .xaml.cs
StudentData _studentData = new StudentData { StudentFirstName = "John", StudentGradePointAverage = 3.5};
public MainWindow()
{
InitializeComponent();
this.DataContext = _studentData;
}
why we need to use INotifyPropertyChanged in this case?
It is not my code.
You need INotifyPropertyChanged if you want a wpf form to be automatically updated when a property changes through code. Also some controllers might want to know if edits have been made in order to enable/disable a save-button, for instance. You also might be displaying the same property on different views; in this case INotifyPropertyChanged helps to immediately update the other view when you edit a property.
If you think that your form behaves well without INotifyPropertyChanged, then you can drop it.
Note that binding works even without INotifyPropertyChanged. See: Why does the binding update without implementing INotifyPropertyChanged?
I would implement the properties like this. In some rare cases it can help to avoid endless circular updates. And it is more efficient by the way.
private string _firstName;
public string StudentFirstName
{
get { return _firstName; }
set
{
if (value != _firstName) {
_firstName = value;
OnPropertyChanged("StudentFirstName");
}
}
}
Starting with C#6.0 (VS 2015), you can implement OnPropertyChanged like this:
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
When you bind to a property of StudentData such as the StudentFirstName then the binding class tests to see if the StudentData instance provides the INotifyPropertyChanged interface. If so then it will hook into the PropertyChanged event. When the event fires and it fires because of the StudentFirstName property then it knows it needs to recover the source value again because it has changed. This is how the binding is able to monitor changes in the source and reflect them in the user interface.
If you do not provide the INotifyPropertyChanged interface then the binding has no idea when the source value changes. In which case the user interface will not update when the property is changed. You will only see the initial value that was defined when the binding was first used.
It does need to be implemented in order for binding to work but that doesn't mean you always have to do it yourself. There are other options like Castle Dynamic Proxy (which wraps your classes in a proxy and injects INPC into all virtual properties) and Fody (which adds it to the IL in a post-processing step). It's also possible to implement yourself while at the same time reducing code bloat, as demonstrated in my answer to this question.

Why does Binding to an instance of my class not work when this.DataContext = this

I have a class that defines a preconfigured socket and all the methods needed to access and control a specific piece of equipment remotely. Part of the class includes an instance of an object that holds the current status of various aspects of the equipment. Each item in the object reports updates using INotifyPropertyUpdate. When I plug it into my test program, all of the methods are called and execute properly, but the only way I seem to be able to get updates of the status to show in the UI is when the DataContext is set to the "Current" object inside the instance of the class. If I set the DataContext to the instance of the class, or to the UI, I stop getting updates in the UI. I would like to be able to use the UI as the DataContext and then bind in the XAML using {Binding Path=InstanceOfMyClass.Current.StatusItemA}
The pertinent parts of the classes in question:
public MyClass : Socket, INotifyPropertyChanged // INotifyPropertyChanged is also used to notify changes in other parts of the class
{
public MyClass : base(//socket configuration info here)
{}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private CurrentStatusObject _current = new CurrentStatusObject();
public CurrentStatusObject Current
{
get { return _current; }
set
{
if (_current != value)
{
_current = value;
NotifyPropertyChanged();
}
}
}
// other methods and properties etc.
}
// this is the Current status object
public class CurrentStatusObject : object, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private string _statusItemA;
public string StatusItemA
{
get { return _statusItemA; }
set
{
if (_statusItemA != value)
{
_statusItemA = value;
NotifyPropertyChanged(); // not necessary to pass property name because of [CallerMemberName]
}
}
}
This works:
c#
this.DataContext = this.InstanceOfMyClass.Current;
XAML
<Label Content="{Binding Path=StatusItemA, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
This does not work, but I want it to:
c#
this.DataContext = this;
XAML
<Label Content="{Binding Path=InstanceOfMyClass.Current.StatusItemA, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
Nor does this:
c#
this.DataContext = this.InstanceOfMyClass;
XAML
<Label Content="{Binding Path=Current.StatusItemA, Mode=OneWay, UpdateSourceTrigger}"/>
I didn't see any answers when searching, but sometimes my research skills fail me. Any help would be appreciated. I enjoy learning new ways of coding. This is my first c# or wpf project. All my projects previous to this have been vb.net in WinForms so I'm at a slight handicap with the learning curve. I would like to learn the correct way to reach my goals for this project, which at this point is simply completing the UI.
The CurrentStatusObject notifies changes internally and that does work. The problem is that the changes are only reflected in the User Interface if I set the DataContext for the UI to that one object. I want to be able to set the DataContext to include a wider scope. I would be happy if I could use the instance of MyClass as the DataContext, but that is not working right now.
The question is Why? and How do I get it to work (using correct practices)?
I presume you have a typo...
public _Current Current = new _Current();
But if so this is a field and not a property. If you change it to this then the binding might work
private _Current _current = new _Current();
public _Current Current
{
get
{
return _current;
}
}
B.T.W: it is not standard to use underscore as part of your class name. Removing it should be all you need
You can only bind to Properties, never fields.
InstanceOfMyClass and Current needs to be declared as properties before you can bind to it (to make DataContext = this work).
As an aside, MVVM dictates that you shouldn't be using your View code-behind as the view model. You should have a separate class for that.

Is there a WPF check box control toggle event?

Considering WPF controls, how do I know if a check box's value has changed (toggled)?
I know there are the common Checked, Unchecked, Clicked events, but how about an event for when the value changes, regardless of how it was changed?
I looked through the events and I didn't find anything, but maybe I'm missing the obvious (as it has happened many times in the past).
You can just bind IsChecked Dependency Property to a boolean. On that binded property setter you can manipulate what you want (independently if it's setting it to true or false). That works just as expected.
On your view:
<Grid>
<CheckBox ... IsChecked="{Binding ShowPending}"/>
</Grid>
On your DataContext ViewModel or CodeBehind.
private bool showPending = false;
public bool ShowPending
{
get { return this.showPending; }
set
{
//Here you mimic your Toggled event calling what you want!
this.showPending = value;
}
}
I know this already has an accepted answer, but binding is a bit overkill on this.
Just write one event handler and wire it to both the Checked and Unchecked events then check the IsChecked property inside your event handler.
Going off Randolf's answer, just create a class representing your window.
In the new class, create a property called BlahIsChecked. Implement the INotifyPropertChangedEvent in the class and in the setter of the the new property, fire the event with the property name.
class Blah : INotifyPropertyChanged
{
// Used for triggering the event
public event PropertyChangedEventHandler PropertyChanged;
// Called when the property changes
protected void OnPropertyChanged(String propertyName)
{
// Retrieve handler
PropertyChangedEventHandler handler = this.PropertyChanged;
// Check to make sure handler is not null
if(handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private bool _blahIsChecked;
public bool BlahIsChecked
{
get {
return _blahIsChecked;
}
set {
_blahIsChecked = value;
OnPropertyChanged("BlahIsChecked);
}
}
}
Now, go to your wpf class and say this.DataContext = new MainModel(); You can do this in WPF or c#.
Now in your checkbox xaml do the following
<checkbox Checked="{Binding BlahIsChecked, Mode=TwoWay}"/>
I did this from memory but should get you started. Good luck.
Your best option is probably the IsChecked property. But if you require an event, you can look at creating a DependencyPropertyDescriptor and registering a handler with the AddValueChanged method.
I think this is about as close as you'll get to an evented notification that the check box's value has changed. Creating the descriptor and adding the handler looks something like this:
var dpd = DependencyPropertyDescriptor.FromProperty(CheckBox.IsChecked, typeof(CheckBox));
dpd.AddValueChanged(...);

How to have bindable properties of a UserControl which work with OnPropertyChanged

I have a simple usercontrol (WinForms) with some public properties. When I use this control, I want to databind to those properties with the DataSourceUpdateMode set to OnPropertyChanged. The datasource is a class which implements INotifyPropertyChanged.
I'm aware of the need to create bindings against the properties and I'm doing that.
I assumed that my usercontrol would have to implement an interface, or the properties would need to be decorated with some attribute, or something along those lines.But my research has come up blank.
How should this be accomplished? At the moment I'm doing it by calling OnValidating() in my usercontrol whenever a property changes, but that doesn't seem right.
I can get validation to happen if I set the CausesValidation to true on the usercontrol, but that's not very useful to me. I need to validate each child property as it changes.
Note this is a WinForms situation.
EDIT: Evidently I have no talent for explanation so hopefully this will clarify what I'm doing. This is an abbreviated example:
// I have a user control
public class MyControl : UserControl
{
// I'm binding to this property
public string ControlProperty { get; set; }
public void DoSomething()
{
// when the property value changes, the change should immediately be applied
// to the bound datasource
ControlProperty = "new value";
// This is how I make it work, but it seems wrong
OnValidating();
}
}
// the class being bound to the usercontrol
public class MyDataSource : INotifyPropertyChanged
{
private string sourceProperty;
public string SourceProperty
{
get { return sourceProperty; }
set
{
if (value != sourceProperty)
{
sourceProperty = value;
NotifyPropertyChanged("SourceProperty");
}
}
}
// boilerplate stuff
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public class MyForm : Form
{
private MyControl myControl;
public MyForm()
{
// create the datasource
var dataSource = new MyDataSource() { SourceProperty = "test" };
// bind a property of the datasource to a property of the usercontrol
myControl.DataBindings.Add("ControlProperty", dataSource, "SourceProperty",
false, DataSourceUpdateMode.OnPropertyChanged); // note the update mode
}
}
(I have tried this using a BindingSource, but the result was the same.)
Now what I want to happen is that when the value of MyControl.ControlProperty changes, the change is immediately propagated to the datasource (the MyDataSource instance). To achieve this I call OnValidating() in the usercontrol after changing the property. If I don't do that, I have to wait until validation gets triggered by a focus change, which is the equivalent of the "OnValidation" update mode, rather than the desired "OnPropertyUpdate" validation mode. I just don't feel like calling OnValidating() after altering a property value is the right thing to do, even if it (kind of) works.
Am I right in assuming the calling OnValidating() is not the right way to do this? If so, how do I notify the datasource of the ControlProperty change?
I think I've got this figured out. I didn't understand how change notifications were sent from control to bound datasource.
Yes, calling OnValidating() is the wrong way.
From what I've pieced together, there are two ways a control can notify the datasource that a property has changed.
One way is for the control to implement INotifyPropertyChanged. I had never done this from the control side before, and I thought only the datasource side of the binding had to implement it.
When I implemented INotifyPropertyChanged on my user control, and raised the PropertyChanged event at the appropriate time, it worked.
The second way is for the control to raise a specific change event for each property. The event must follow the naming convention: <propertyname>Changed
e.g. for my example it would be
public event EventHandler ControlPropertyChanged
If my property was called Foo, it would be FooChanged.
I failed to notice the relavent part of the MSDN documentation, where it says:
For change notification to occur in a
binding between a bound client and a
data source, your bound type should
either:
Implement the INotifyPropertyChanged
interface (preferred).
Provide a change event for each
property of the bound type.
This second way is how all existing WinForms controls work, so this is how I'm doing it now. I use INotifyPropertyChanged on my datasource, but I raise the Changed events on my control. This seems to be the conventional way.
Implementing the INotifyPropertyChanged interface is very simple. Here is a sample that shows an object with a single public field...
public class Demo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
private string _demoField;
public string DemoField
{
get {return demoField; }
set
{
if (value != demoField)
{
demoField = value;
NotifyPropertyChanged("DemoField");
}
}
}
}
Then you would create a Binding instance to bind a control property to a property (DemoField) on your source instance (instance of Demo).

How to bind to a local property in Silverlight without using a DataContext?

I have a small question but have been finding quite a few different, and mostly ambiguous, answers:
I have the following user control and I am trying to bind to a public property within that control (Events). Everyone says that I have to use the data context, however, I don't really want to do that... I just want to bind to the property from within the control's
XAML...
The requirement is that the binding has to be 2 way so any changes in the ui will be reflected in the property (or rather the collection) it is bound to. Each Event object within that collection also implements INotifyPropertyChanged the same way as this control...
Any ideas would be greatly appreciated!
public partial class EventEditorWindow : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<Event> events;
public ObservableCollection<Event> Events
{
get { return this.events; }
set
{
if( this.events != value )
{
this.events = value;
this.RaisePropertyChanged("Events");
}
}
}
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
this.VerifyPropertyName(propertyName);
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(string propertyName)
{
var currentObjectType = this.GetType();
if (currentObjectType.GetProperty(propertyName) == null)
{
throw new ArgumentException("Property not found", propertyName);
}
}
}
Thanks,
Bleepzter.
In the constructor, set DataContext = this. That will effectively make your code behind your DataContext. AFAIK, you can't completely avoid making something the DataContext.
You could use a RelativeSource so you don't need the DataContext:
{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type EventEditorWindow }}, Path=Events}
I use this cheat sheet from time to time.
EDIT Oops this is WPF syntax. See this post to have a look at this post to solve it in Silverlight

Categories

Resources