How can I get bindings to update when the value is changed? - wpf

I'm trying to understand WPF binding. As simple as it gets:
I have a ClassWithProperty that has a public uint Prop1.
The main window has a public ClassWithProp object and uses it for data context. This is set in the main Windows's constructor:
this.ClassWithProp = new ClassWithProp();
this.DataContext = this.ClassWithProp;
ClassWithProp's default constructor sets Porp1 value to 1.
The main windows contains a label:
<Label Content="{Binding Prop1}" ... />
It also contains a button that, when click, sets the ClassWithProp.Prop1 to 2.
When the window first appears, the label correctly shows 1. When the button is clicked the property's value is changed to 2, but the lable does not refresh.
Sorry - probably obvious but I'm a novice in WPF:
Why doesn't the bound label update when the undelying property changes?

Your ClassWithProperty needs to implement the INotifyPropertyChanged interface (which has just the one event on it, PropertyChanged), this way the WPF binding subsystem can listen for property changes and update the value. When you have changed the value of a property, you raise the event.
Here is an example:
pulic class ClassWithProperty : INotifyPropertyChanged
{
public uint Prop1
{
get { return _prop1; }
set
{
_prop1 = value;
OnPropertyChanged("Prop1");
}
}
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
private uint _prop1;
}

Implement INPC.
Also read the overview, it probably answers more than 90% of questions people have about data binding.

Related

Dependency Properties: 'Freezing' DataBinding until user commits

I am trying to implement a system that would enhance WPF's DataBinding engine.
My prime concern right now is the following:
I would like to be able to 'freeze' the DependencyProperty - stop it from updating from the Model - once the user has started to input something in the UI.
Consider the following example:
I am binding a TextBox.TextProperty to some property on my ViewModel.
The user started typing inside the textbox, and the moment he starts to type, I want to prevent the ViewModel from updating the View.
Only after commiting the change, the user will see the update in the model.
I am trying to create some kind of MultiBinding using a a bool-flag which will tell me whether we need to update the GUI or not, but other than that I don't know how to continue.
Any help would be appreciated!
You can do that by setting UpdateSourceTrigger to PropertyChanged in binding and then set the flag accordingly in the setter of bounded property. PropertyChanged value Updates the binding source immediately as user will input any character.
xaml
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"/>
ViewModel
public class ViewModel:INotifyPropertyChanged
{
bool stopUpdate;
string name;
public string Name
{
get
{ return name;}
set
{
name = value;
stopUpdate=true;
OnPropertyChanged("Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}

how do I see if Combobox Selectionchanged event is triggered manually or by binding (WPF - MVVM)

I'm in a situation where I need to empty a property in my model whenever someone changes a value in a combobox.
A side effect of this is, that whenever I change the value of the Combobox-Bound variable, the Combobox SelectionChanged event is triggered.
Is there anyway to know who is triggering this event. I'd like to know if it is triggered manually or by binding.
I'm looking in to the sender, but they look about the same.
Thank you,
This is a prime example of why WPF developers should use the MVVM design pattern. By relying on SelectionChanged events to control the flow of your code, you're losing your own control over what should happen and are resorting to having to know how an event is being triggered so that you can respond. That's reactive, not pro-active. As a developer, you should always know what can and will affect your code flow.
Rather, set your WPF page or control's DataContext to a viewmodel class that wraps your model and bind your combobox to the ViewModel properties, specifically SelectedItem in this case. This will simplify your coding immensely.
ViewModel
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<string> Names = new ObservableCollection<string>();
private string _selectedName;
private YourModel _model;
public ViewModel(YourModel model)
{
_model = model;
}
public string SelectedName
{
get { return _model.SelectedName; }
set
{
_model.SelectedName = value;
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML
<ComboBox ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedName}" />
Dropodownclosed is the event I best use here instead of selectionchanged. This will make sure that the event is triggered not by the data that is bound but by user interface interaction.

WinForms - how to set property value in code and get bound control to update?

I have a WinForms app that contains several comboboxes, numericupdown controls and checkboxes. I also have a data class that exposes several properties and these controls are bound to those properties. Now I need the ability to restore the default values for each of these properties and have all of the bound controls update to reflect the change. For example, one of my comboBox controls is bound to an enum that contains (Red, Blue, Yellow and Green). The default value for this property is set to Blue in my data class constructor. When my app starts up, the combobox that's bound to this property correctly displays Blue as the default selected item. If a user were to select a different color and then decide that they want to revert back to the default color, I need a way to change the property value back to Blue and get the bound control to show that. I can set the property value in code but the problem is that my comboBox doesn't update to reflect the change. I'm guessing there's some kind of change notification mechanism that I need to implement in my data class but I'm not sure what that would be. Any ideas would be much appreciated. Thanks very much!
You'll need to implement INotifyProperyChanged in your data class like:
public class Coloring : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
if (_name != value) {
_name = value;
OnPropertyChanged("Name");
}
}
}
// Do this for all your properties
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
If you are using a BindingSource you can call its ResetBindings method.
myBindingSource.ResetBindings(false);

What's the concept behind INotifyPropertyChanged?

All the examples of Silverlight using MVVM use interface named IPropertyChanged. What is the concept behind it and why do we need to raise an event whenever we set some value?
Eg:-
public class UserNPC:INotifyPropertyChanged
{
private string name;
public string Name {
get { return name; }
set { name = value; onPropertyChanged(this, "Name"); }
}
public int grade;
public int Grade {
get { return grade; }
set { grade = value; onPropertyChanged(this, "Grade"); }
}
// Declare the PropertyChanged event
public event PropertyChangedEventHandler PropertyChanged;
// OnPropertyChanged will raise the PropertyChanged event passing the
// source property that is being updated.
private void onPropertyChanged(object sender, string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
}
What is the exact purpose of INotifyPropertyChanged?
You have the following dependencies:
View → Binding → Model
Now, the concept is as following:
If some data in your Model object changes, you are required to raise the PropertyChanged event. Why? Because the Binding object has registered a method with the data object's PropertyChanged event.
So all you have to do when something changes within your Model object is to raise the event and you are done.
When you do that, the Binding object gets notified about the change through your event. The Binding object in turn lets the View object know that something happened. The View object then can update the UI if necessary.
Code example
Here you have a compilable example. Set a few breakpoints, step through the code with F11 and see what happens behind the scenes. Note that this example has the following dependency: View → Model. I left out the Binding object.
using System;
using System.ComponentModel;
namespace INotifyPropertyChangedDemo
{
class Program
{
static void Main(string[] args)
{
// Create 2 listeners.
View1 view1 = new View1();
View2 view2 = new View2();
// Create 1 data object.
Model model = new Model();
// Connect listener with data object.
model.PropertyChanged += new PropertyChangedEventHandler(view1.MyPropertyChangedEventHandler);
model.PropertyChanged += new PropertyChangedEventHandler(view2.MyPropertyChangedEventHandler);
// Let data object publish change notification.
model.FirstName = "new name";
// Check whether all listeners got notified.
// ... via console.
}
public class Model : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string firstName;
public string FirstName
{
get { return firstName; }
set
{
if (firstName != value)
{
firstName = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
}
}
}
}
}
public class View1
{
public void MyPropertyChangedEventHandler(object source, PropertyChangedEventArgs arg)
{
Console.WriteLine("Listener 1: Changed Property: {0}", arg.PropertyName);
string newValue = ((Model) source).FirstName;
Console.WriteLine("Listener 1: Changed Property Value: {0}", newValue);
}
}
public class View2
{
public void MyPropertyChangedEventHandler(object source, PropertyChangedEventArgs arg)
{
Console.WriteLine("Listener 2: Changed Property: {0}", arg.PropertyName);
string newValue = ((Model)source).FirstName;
Console.WriteLine("Listener 2: Changed Property Value: {0}", newValue);
}
}
}
}
MVVM in WPF & Silverlight is implemented by binding UI elements to the view model. When the view model changes, though, how will the UI know to update itself?
INotifyPropertyChanged simply exposes an event to which the UI can "listen," so when a control "hears" that the property to which it is bound has changed, it can "update itself."
For example, say you have a TextBlock that shows a stock price, and it is bound to the string Price property of a view model. The view model, in turn, uses a service to update stock prices every 30 seconds. So, every 30 seconds the Price property changes: 30 seconds ago it was "$29.20" now it is "$29.12" and 30 seconds from now it will be "$28.10". The TextBlock binding is applied when the TextBlock is loaded, but not every time the Price changes. If, however, you implement INotifyPropertyChanged and raise the event for property "Price" in the Price setter, then the TextBlock can wire into the event and thereby "know" when to go back and "re-read" the Price property and update the displayed text.
Most Silverlight controls listen out for changes to the data they display by simply subscribing to the PropertyChanged events.
e.g. the control does something like this behind the scenes:
public void Loaded()
{
if (myDataObject is INotifyPropertyChanged)
{
(myDataObject as INotifyPropertyChanged).PropertyChanged +=new PropertyChangedEventHandler(onPropertyChanged);
}
}
That is also why ObservableCollection is used instead of simpler Lists in Silverlight Apps. They implement INotifyPropertyChanged so controls that display collections are able to see changes occurring to the list as well as to individual items in a list.
I had created a 3-tiered program recently for fun, and wanted to make sure all the parts where as separated as possible.
In my GUI, the user could type in a name however they wanted, however, my business class had logic in there to change all names to Title Case. This worked, however, the GUI is never told about this update that the business class did.
So my work around at that time was simple...but did not look right. Something like the following
var _person = new Person();
// In some form event handler like button click
_person.Name = txtName.Text;
txt.Name.Text = _person.Name;
This did the job of updating the GUI while keeping it separate from the business logic. What I wanted was to create an event that would fire when the business logic changed the value from what was typed in the GUI, and the GUI would listen in on that event.
So now I would have something like...
var _person = new Person();
// In some form event handler like button click
_person.Name = txtName.Text;
// In the GUI class
public void OnInternalPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
txtName.Text = _person.Name;
}
NOTE: I am not doing this on all of the property changes...just the ones that deviate from what the user expects it to be...changing all lowercase name to Title Case, and showing that to the user.

how do i get a wpf window to refresh?

I am building a simple roulette app. The player(UI) puts together a list of bets and submits them to the table object to be evaluated and paid out. I've got the code to work and the game process goes smoothly. The problem is that after a turn I can't get the player balance(textblock) or the betlist(listview) to update. Is there some sort of global window refresh command I am missing, or do I have to manually set each of these to update somehow?
WPF can take care of updating these values for you automatically, but you have to let it know when things have changed. Typically, this is done by using DependencyProperties on your model objects, but it can also be done by implementing INotifyPropertyChanged. In either case, when you update a property's value, the PropertyChanged event gets called; WPF automatically subscribes to this event when it binds to a value, and will update the UI when a change occurs. Without this notification, WPF won't check to see if the values in your object have changed, and you won't see the change reflected on the screen.
What about implementing INotifyPropertyChanged, and bind the balance and the betlist to the controls you are using?
Something like:
public class Player : INotifyPropertyChanged
{
private int _balance;
#region Properties
public int Balance
{
get { return this._balance; }
set
{
if (this._balance != value)
{
this._balance = value;
NotifyPropertyChanged("Balance");
}
}
}
public BindingList<Bet> BetList { get; set; }
#endregion // Properties
private void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
public class Bet
{
// some code
}
For the binding list you wouldn't need to implement anything since it implements an interface that notifies changes to whatever is bound to (IRaiseItemChangedEvents). But then again you could be using a different approach.

Resources