INotifyPropertyChanged Implemented now what? - c#

I am trying to create a list box that displays a set of data that will be updated over time. I have a simple list box:
<ListBox Name="lbRegisters" ItemsSource="{Binding}" />
And I have defined a class for my objects:
public class register : INotifyPropertyChanged
{
private int address;
public int Address { get { return address; } }
private int value;
public int Value
{
get{ return value; }
set{
this.value = value;
OnValueChanged("Value");
}
}
public register(int a)
{
address = a;
value = 0;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnValueChanged(string name){
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public override string ToString()
{
return address + ": " + value;
}
}
And I hold a list of these in an ObservableCollection<register>. Then set the ListBox.ItemsSource=registerslist; in the CodeBehind. The list is initialized and the inital data displays correctly.
Now what do I need to do to get my ListBox to update when a "register.Value" changes. The event handler is called but there nothing is subscribed to the event.
I guess I need to trigger something in the ListBox or ObservableCollection to tell the GUI to update. I have read dozens of posts of a similar problem but they all seem to indicate that once you have implemented INotofyPropertyChanged it just automagicaly works.
What is the next step?

The problem is on your ToString() function. Yes it could be used to display complex string in ListView items but it is not a proper way to bindings because ListView does not knows when part of this string was changed.
Do the following:
1. Declare property on register class like
public string AddressValue
{
get { return address + ": " + value; }
}
2. Add OnValueChanged("AddressValue") in value and address setters like:
public int Value
{
get{ return value; }
set{
this.value = value;
OnValueChanged("Value");
OnValueChanged("AddressValue")
}
}
3. Declare you ListBox with ItemTemplate like:
<ListBox x:Name="lbRegisters" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding AddressValue}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
So the idea is adding new property to register class which will be updated when address and value changed. And bind list item text to this property.

I think understand the spirit of what you're asking (even though it's obviously unfinished ATM). I'd recommend using something that inherits from DependencyObject and using dependency properties. Use the propdp snippet in Visual Studio. It will save you a ton of boilerplate code and wiring.

Related

WPF UserControl Binding to Collection

I have a listBox on my UI, and a backing collection in my viewmodel.
I have been trying to databind/datatemplate it so that if a new item is added in the collection, a new user control is added to the UI. However, I cannot get this to work FULLY.
To clarify, I can get the right number of controls to appear on the UI for the number of items in the collection, but cannot then bind the properties of the user control to the item in the collection.
This is only me trying to figure it out initially to then make this vastly more complex later on, perhaps good that I have this issue now..
DataBinding WPF Code:
<ListBox x:Name="SubSystemList" ItemsSource="{Binding Path=SubSystems}">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type src:SubSystem}">
<controls:SubSystem DeviceCount="{Binding Path=DeviceCount}" SystemName="{Binding Path=Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Properties from the usercontrol that i am trying to render into:
public string SystemName
{
get { return (string) GetValue(SubSystenNameProperty); }
set { SetValue(SubSystenNameProperty, value); }
}
public int DeviceCount
{
get { return (int) GetValue(DeviceCountProperty); }
set { SetValue(DeviceCountProperty, value); }
}
I am reluctant to post much more code (straight away..) as it would only become more difficult to follow.
The Debug WriteLine's in the setters do not fire at all (breakpoints dont get hit). Which makes me think its a path/binding issue.
Any help would be much appreciated.
EDIT
src:SubSystem code: (removed additional properties for clarity)
public class SubSystem
{
[XmlAttribute("Name")]
[DataMember]
public string Name
{
get { return _name; }
set { _name = value; }
}
[DataMember]
public int DeviceCount
{
get { return _deviceCount; }
set { _deviceCount = value; }
}
}
DependencyProperties on usercontrols:subsystem
public static readonly DependencyProperty DeviceCountProperty = DependencyProperty.Register(
nameof(DeviceCount), typeof(int), typeof(SubSystem));
public static readonly DependencyProperty SubSystenNameProperty = DependencyProperty.Register(
nameof(SystemName), typeof(string), typeof(SubSystem));
Another Edit
Further head scratching revealed this in the output logs:
em.Windows.Data Error: 40 : BindingExpression path error: 'SystemName' property not found on 'object' ''SubSystem' (HashCode=32985660)'. BindingExpression:Path=SystemName; DataItem='SubSystem' (HashCode=32985660); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')
Makes me think theres something happening in the user control code behind/bindings that it doesnt like, amusing thing is, after i was getting confused, i changed the name of the usercontrol to subsystemcontrol, and made sure all the binding names in the uc were correct. so not sure why its doing this entirely.. further digging on going.
You seem to have your dependency property declaration wrong.
As example, the full declaration of a string SystemName dependency property would look like this:
public static readonly DependencyProperty SystemNameProperty =
DependencyProperty.Register("SystemName", typeof(string), typeof(SubSystem));
public string SystemName
{
get { return (string)GetValue(SystemNameProperty); }
set { SetValue(SystemNameProperty, value); }
}
Note that there shouldn't be anything else then GetValue/SetValue in the property wrapper. If you need to get notified about property value changes, you can register a PropertyChangedCallback via property metadata with another overload of the Register method.
I think that you should:
Implement InotifyPropertyChanged interface in your SubSystem class.
SetValue and GetValue of the DependencyProperties SystemName and DeviceCount
public class SubSystem:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[XmlAttribute("Name")]
[DataMember]
public string Name
{
get { return _name; }
set {
_name = value;
OnPropertyChanged("Name");
}
}
[DataMember]
public int DeviceCount
{
get { return _deviceCount; }
set {
_deviceCount = value;
OnPropertyChanged("DeviceCount");
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
In your SubSystem class Name property should be replaced by SystemName
[XmlAttribute("SystemName")]
[DataMember]
public string SystemName
{
get { return _name; }
set { _name = value; }
}

Two Way binding to a Dependency Property in a User Control and call a method

I know, title is a little confusing so let me explain. I have a user control that has a dependency property. I access this dependency property with a regular property called Input. In my view model I also have a property called Input. I have these two properties bound together in XAML using two-way binding as shown below:
<uc:rdtDisplay x:Name="rdtDisplay" Input="{Binding Input, Mode=TwoWay}" Line1="{Binding myRdt.Line1}" Line2="{Binding myRdt.Line2}" Height="175" Width="99" Canvas.Left="627" Canvas.Top="10"/>
Okay in my view model, I call a method whenever the value of Input is changed as shown in my property:
public string Input
{
get
{
return input;
}
set
{
input = value;
InputChanged();
}
}
The problem with this is that when I set the value of Input in my view model it only updates the value of the variable input as per my setter in my property. How can I get this to update back to the dependency property in the user control? If I leave the code input = value; out then I get a compilation error.
I need something like this:
public string Input
{
get
{
return UserControl.Input;
}
set
{
UserControl.Input = value;
InputChanged();
}
}
If I make the Input property in my view model look like this:
public string Input
{
get; set;
}
then it works, however, I am unable to call the InputChanged() method that I need to call when the Property is changed. All suggestions are appreciated.
Implement INotifyPropertyChanged in your ViewModel
public class Sample : INotifyPropertyChanged
{
private string input = string.Empty;
public string Input
{
get
{
return input;
}
set
{
input = value;
NotifyPropertyChanged("Input");
InputChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
In your case, you can do it in the code behind of your usercontrol

ListPicker Data Binding and INotifyPropertyChanged

So I have two ListPickers, Device Type and Device Name.
If I select Tablet in Device Type, I want the Device Name ListPicker to show options like Ipad, Dell Venue 8, etc.
If I select Phone in Device Type, I want the Device Name ListPicker to show options like Iphone, Samsung Galaxy, etc and so on.
So how can I go about making a data binding between these two ListPickers and also implement INotifyPropertyChanged so changes in one ListPicker are dynamically reflected in the other?
You can do the following :
In your xaml :
<toolkit:ListPicker x:Name="DeviceType" ItemSource="{Binding DeviceTypeList}" SelectedItem="{Binding SelectedDeviceType, Mode=TwoWay}"/>
<toolkit:ListPicker x:Name="DeviceName" ItemSource="{Binding DeviceNameList}" />
In your code :
public class ClassName : NotifyChangements
{
private YourType selectedDeviceType;
public YourType SelectedDeviceType
{
get { return selectedDeviceType; }
set
{
selectedDeviceType = value;
NotifyPropertyChanged("SelectedDeviceType");
MAJDeviceName();
}
}
// Later in code.
public void MAJDeviceName()
{
// Add code here that fill the DeviceNameList according to the SelectedDeviceType.
}
}
And for the NotifyChangements class :
using System.ComponentModel;
using System.Runtime.CompilerServices;
public class NotifyChangements : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
public bool NotifyPropertyChanged<T>(ref T variable, T valeur, [CallerMemberName] string property = null)
{
if (object.Equals(variable, valeur)) return false;
variable = valeur;
NotifyPropertyChanged(property);
return (true);
}
}
You also have to add the List<YourType> DeviceNameList as an attribute and call the NotifyPropertyChanged("DeviceNameList") in the setter of that attribute in order to make it bind the datas.
Moreover, I'll let you change the variable and type names as you haven't provided any code samples !

Elegant way to implement INotifyPropertyChanged across many controls

I'm building a WPF application and I'm slowly uncovering some of the joys and also the frustrations of using WPF. My latest question involves updating the UI using INotifyPropertyChanged
My app has stacked UserControls with each UserControl containing multiple controls, so overall there are hundreds of controls which update every second providing live data. In order to update all controls I'm using something similar to below which does currently work as intended.
namespace ProjectXAML
{
public partial class ProjectX : UserControl, INotifyPropertyChanged
{
#region Declare Getter/Setter with INotifyPropertyChanged groupx3
private string m_group1Text1;
public string group1Text1
{
get
{
return m_group1Text1;
}
set
{
m_group1Text1 = value;
NotifyPropertyChanged("group1Text1");
}
}
private string m_group1Text2;
public string group1Text2
{
get
{
return m_group1Text2;
}
set
{
m_group1Text2 = value;
NotifyPropertyChanged("group1Text2");
}
}
private string m_group2Text1;
public string group2Text1
{
get
{
return m_group2Text1;
}
set
{
m_group2Text1 = value;
NotifyPropertyChanged("group2Text1");
}
}
private string m_group2Text2;
public string group2Text2
{
get
{
return m_group2Text2;
}
set
{
m_group2Text2 = value;
NotifyPropertyChanged("group2Text2");
}
}
private string m_group3Text1;
public string group3Text1
{
get
{
return m_group3Text1;
}
set
{
m_group3Text1 = value;
NotifyPropertyChanged("group3Text1");
}
}
private string m_group3Text2;
public string group3Text2
{
get
{
return m_group3Text2;
}
set
{
m_group3Text2 = value;
NotifyPropertyChanged("group3Text2");
}
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
/// Notifies the property changed.
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
#endregion
}
}
My questions are:
Is there a more elegant way to raise PropertyChanged events for lots of controls rather than lots of get/set code?
Is there a way to raise 1 PropertyChanged event covering the whole UserControl containing multiple controls instead of a separate event for every control? Is there a better method than what I'm attempting?
In strict reference to this part of your question..."Is there a way to raise 1 PropertyChanged event covering the whole UserControl containing ".
Yes, you can raise a PropertyChanged notification which says all my properties on my object are updated.
Use:
NotifyPropertyChanged(null);
then this informs the listener of INotifyPropertyChanged that all properties have changed on an object.
This isn't normally used...and can be abused....and cause inefficient updates e.g. if you were only changing a few properties and used that.
But you could argue the case for using it if you have lots of properties in your object, that you were always changing anyway at the same time...and you wanted to collapse lots of individual notifications into 1 that was raised after you had modified all properties.
Example use case (i.e. presumes you are updating all your groups in some way):
void UpdateAllGroupTextProperties()
{
group1Text1 = "groupA";
group1Text2 = "groupA2";
group2Text1 = "groupB";
group2Text2 = "groupB2";
group3Text1 = "groupC";
group3Text2 = "groupC2";
NotifyPropertyChanged(null);
}
For point 1 if you are using VS 2012 you can do the below
private void SetProperty<T>(ref T field, T value, [CallerMemberName] string name = "")
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
field = value;
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
and then you can use your set property method without having to hard code the name of the properties.
Note the above code is an except of the below link
http://danrigby.com/2012/03/01/inotifypropertychanged-the-net-4-5-way/
Use the design pattern model view controler. So the model will raise the changes for you. Together with MVVM the controls will see with its dependency objects the changes and view them automatically.

Implementing INotifyPropertyChanged for nested properties

I have a Person class:
public class Person : INotifyPropertyChanged
{
private string _name;
public string Name{
get { return _name; }
set {
if ( _name != value ) {
_name = value;
OnPropertyChanged( "Name" );
}
}
private Address _primaryAddress;
public Address PrimaryAddress {
get { return _primaryAddress; }
set {
if ( _primaryAddress != value ) {
_primaryAddress = value;
OnPropertyChanged( "PrimaryAddress" );
}
}
//OnPropertyChanged code goes here
}
I have an Address class:
public class Address : INotifyPropertyChanged
{
private string _streetone;
public string StreetOne{
get { return _streetone; }
set {
if ( _streetone != value ) {
_streetone = value;
OnPropertyChanged( "StreetOne" );
}
}
//Other fields here
//OnPropertyChanged code goes here
}
I have a ViewModel:
public class MyViewModel
{
//constructor and other stuff here
private Person _person;
public Person Person{
get { return _person; }
set {
if ( _person != value ) {
_person = value;
OnPropertyChanged( "Person" );
}
}
}
I have a View which has the following lines:
<TextBox Text="{Binding Person.Name, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged />
<TextBox Text="{Binding Person.Address.StreetOne, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged />
Both values show up in the text box ok when the view loads.
Changes to the first text box will fire OnPropertyChanged( "Person" ) in MyViewModel. Great.
Changes to the second text box ("Person.Address.StreetOne") does NOT fire OnPropertyChanged( "Person" ) inside MyViewModel. Meaning it doesn't call the Person object's SET method. Not great. Interestingly the SET method of StreetOne inside the Address class is called.
How do I get the SET method of the Person object inside the ViewModel to be called when Person.Address.StreetOne is changed???
Do I need to flatten my data so SteetOne is inside Person and not Address??
Thanks!
While adding 'pass-through' properties to your ViewModel is a fine solution, it can quickly become untenable. The standard alternative is to propagate changes as below:
public Address PrimaryAddress {
get => _primaryAddress;
set {
if ( _primaryAddress != value )
{
//Clean-up old event handler:
if(_primaryAddress != null)
_primaryAddress.PropertyChanged -= AddressChanged;
_primaryAddress = value;
if (_primaryAddress != null)
_primaryAddress.PropertyChanged += AddressChanged;
OnPropertyChanged( "PrimaryAddress" );
}
void AddressChanged(object sender, PropertyChangedEventArgs args)
=> OnPropertyChanged("PrimaryAddress");
}
}
Now change notifications are propagated from Address to person.
Edit: Moved handler to c# 7 local function.
if you want the viewmodel SET to be called you could create a street property
public class MyViewModel
{
//constructor and other stuff here
public string Street{
get { return this.Person.PrimaryAddress.StreetOne; }
set {
if ( this.Person.PrimaryAddress.StreetOne!= value ) {
this.Person.PrimaryAddress.StreetOne = value;
OnPropertyChanged( "Street" );
}
}
}
xaml
<TextBox Text="{Binding Street, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged />
but this solution has its drawbacks. i go with Reeds answer in my projects
How do I get the SET method of the Person object inside the ViewModel to be called when Person.Address.StreetOne is changed???
Why do you want to do this? It should not be required - you only need the StreetOne property changed event to fire.
Do I need to flatten my data so SteetOne is inside Person and not Address??
If you want to actually cause this to trigger, you don't need to flatten it (though that is an option). You can subscribe to the Address's PropertyChanged event within your Person class, and raise the event for "Address" within Person when it changes. This shouldn't be necessary, however.
Since I wasn't able to find a ready-to-use solution, I've done a custom implementation based on Pieters (and Marks) suggestions (thanks!).
Using the classes, you will be notified about any change in a deep object tree, this works for any INotifyPropertyChanged implementing Types and INotifyCollectionChanged* implementing collections (Obviously, I'm using the ObservableCollection for that).
I hope this turned out to be a quite clean and elegant solution, it's not fully tested though and there is room for enhancements. It's pretty easy to use, just create an instance of ChangeListener using it's static Create method and passing your INotifyPropertyChanged:
var listener = ChangeListener.Create(myViewModel);
listener.PropertyChanged +=
new PropertyChangedEventHandler(listener_PropertyChanged);
the PropertyChangedEventArgs provide a PropertyName which will be always the full "path" of your Objects. For example, if you change your Persons's "BestFriend" Name, the PropertyName will be "BestFriend.Name", if the BestFriend has a collection of Children and you change it's Age, the value will be "BestFriend.Children[].Age" and so on. Don't forget to Dispose when your object is destroyed, then it will (hopefully) completely unsubscribe from all event listeners.
It compiles in .NET (Tested in 4) and Silverlight (Tested in 4). Because the code in seperated in three classes, I've posted the code to gist 705450 where you can grab it all: https://gist.github.com/705450 **
*) One reason that the code is working is that the ObservableCollection also implements INotifyPropertyChanged, else it wouldn't work as desired, this is a known caveat
**) Use for free, released under MIT License
There is a spelling mistake in your property change notification:
OnPropertyChanged( "SteetOne" );
should be
OnPropertyChanged( "StreetOne" );

Categories