How can I RaisePropertyChanged on property change? - wpf

Here I added a model to my viewmodel:
public dal.UserAccount User {
get
{
return _user;
}
set
{
_user = value;
RaisePropertyChanged(String.Empty);
}
}
I handle property change event...
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
This is the binding I use:
<TextBox Text="{Binding User.firstname, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />
Why the propertychange event is not triggered on updating view?

PropertyChanged is used to notify the UI that something has been changed in the Model.
Since you're changing an inner property of the User object - the User property itself is not changed and therefore the PropertyChanged event isn't raised.
Second - your Model should implement the INotifyPropertyChanged interface. - In other words make sure UserAccount implements INotifyPropertyChanged, otherwise changing the firstname will not affect the view either.
Another thing:
The parameter RaisePropertyChanged should receive is the Name of the property that has changed. So in your case:
Change:
RaisePropertyChanged(String.Empty);
To
RaisePropertyChanged("User");
From MSDN:
The PropertyChanged event can indicate all properties on the object have changed by using either null or String.Empty as the property name in the PropertyChangedEventArgs.
(No need to refresh all the Properties in this case)
You can read more on the concept of PropertyChanged here

You can invoke a property changed event from another class. Not particularly useful if you have all the sources. For closed source it might be. Though I consider it experimental and not production ready.
See this console copy paste example:
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
public class Program
{
static void Main(string[] args)
{
var a = new A();
a.PropertyChanged += A_PropertyChanged;
var excpl = new Excpl();
excpl.Victim = a;
excpl.Ghost.Do();
}
private static void A_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Console.WriteLine("Event triggered");
}
}
[StructLayout(LayoutKind.Explicit)]
public struct Excpl
{
[FieldOffset(0)]
public A Victim;
[FieldOffset(0)]
public C Ghost;
}
public class A : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
}
public class C : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void Do()
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(""));
}
}
}

Related

Can't extend MVVMLight's ViewModelBase with INotifyPropertyChanged

I had been using my own ViewModelBase and RelayCommand, and recently I installed MVVMLight, as well as PropertyChanged.Fody. It appears that PropertyChanged.Fody doesn't notify if you're using a custom setter, so I do need to implement OnPropertyChanged somewhere myself.
public NotifiableViewModelBase CurrentViewModel
{
get => _currentViewModel;
set
{
// store the previous viewmodel when the viewmodel is changed.
PreviousViewModel = _currentViewModel;
_currentViewModel = value;
OnPropertyChanged();
}
}
This is a member of a class that still inherits from my own ViewModelBase
public abstract class NotifiableViewModelBase : INotifyPropertyChanged
{
protected NotifiableViewModelBase() { }
public virtual string DisplayName
{
get;
protected set;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
}
I'd like to have this class inherit from MVVMLight's ViewModelBase, but if I do that, it says PropertyChanged hides its inherited member, and when I delete that event's declaration (to use the inherited member), I get errors in the OnPropertyChanged method, saying PropertyChanged can only be used on the left hand side of a += or -=.
I've managed to get around this by declaring PropertyChanged as a new event, which allows me to inherit from MVVMLight's ViewModelBase, but this smells improper to me.
Am I "doing it right" using the new event keywords, or is there some answer to how to use PropertyChanged as expected in the OnPropertyChanged?
(or, is there a way to write my custom setter and have the notification work without explicitly calling OnPropertyChanged()?)

Static event for INotifyPropertyChanged [duplicate]

This question already has answers here:
Binding to static property
(12 answers)
Closed 2 years ago.
I had a variable which was not static and INotifyPropertyChanged implemented succesfully. Then I tried to make it global, so turned it a static variable. But this time, INotifyPropertyChanged does not work. Any solution?
INotifyPropertyChanged works on instance properties. One solution is to use a singleton pattern and keep INotifyPropertyChanged, the other is to use your own event to notify listeners.
Singleton example
public sealed class MyClass: INotifyPropertyChanged
{
private static readonly MyClass instance = new MyClass();
private MyClass() {}
public static MyClass Instance
{
get
{
return instance;
}
}
// notifying property
private string privMyProp;
public string MyProp
{
get { return this.privMyProp; }
set
{
if (value != this.privMyProp)
{
this.privMyProp = value;
NotifyPropertyChanged("MyProp");
}
}
}
// INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
}
EDIT: In WPF 4.5, they introduced property changed mechanic for static properties:
You can use static properties as the source of a data binding. The
data binding engine recognizes when the property's value changes if a
static event is raised. For example, if the class SomeClass defines a
static property called MyProperty, SomeClass can define a static event
that is raised when the value of MyProperty changes. The static event
can use either of the following signatures.
public static event EventHandler MyPropertyChanged;
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
Very good example, I used it for some general settings in application, when I want to bind some property online to components
public sealed class DataGridClass:INotifyPropertyChanged
{
private static readonly DataGridClass instance = new DataGridClass();
private DataGridClass() { }
public static DataGridClass Instance
{
get
{
return instance;
}
}
private int _DataGridFontSize {get;set;}
public int DataGridFontSize
{
get { return _DataGridFontSize; }
set { _DataGridFontSize = value;
RaisePropertyChanged("DataGridFontSize");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
Set startup properties:
DataGridClass.Instance.DataGridFontSize = 14(or read from xml)
Bind this to components properties
xmlns:static="clr-namespace:MyProject.Static"
<extgrid:ExtendedDataGrid x:Name="testGrid" ClipboardCopyMode="IncludeHeader" AutoGenerateColumns="False">
<extgrid:ExtendedDataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="FontSize"
Value="{Binding Source={x:Static static:DataGridClass.Instance},
Path=DataGridFontSize, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
</Style>
</extgrid:ExtendedDataGrid.Resources>
When you change this value somewhere in application like "Preferences"->DataGrid FontSize - to it automatically update this property for bindings with UpdateSourceTrigger
private void comboBoxFontSize_DropDownClosed(object sender, EventArgs e)
{
DataGridClass.Instance.DataGridFontSize = Convert.ToInt32(comboBoxFontSize.Text);
}
<ComboBox Grid.Column="1" Grid.Row="0" Height="21" Width="75" Name="comboBoxFontSize" HorizontalAlignment="Left"
VerticalAlignment="Center" DropDownClosed="comboBoxFontSize_DropDownClosed"
ItemsSource="{Binding Source={x:Static commands:ConstClass.ListOfFontSize},Mode=OneWay}"
SelectedItem="{Binding Source={x:Static static:DataGridClass.Instance},Path=DataGridFontSize,
Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>

How INotifyPropertyChanged's PropertyChanged event get assigned?

I have following code and it is working fine.
public partial class MainWindow : Window
{
Person person;
public MainWindow()
{
InitializeComponent();
person = new Person { Name = "ABC" };
this.DataContext = person;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
person.Name = "XYZ";
}
}
class Person: INotifyPropertyChanged
{
string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
OnPropertyChanged("Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string strPropertyName)
{
if(null != PropertyChanged)
{
PropertyChanged(this,
new PropertyChangedEventArgs(strPropertyName));
}
}
}
When I create the "person" object in the constructor of MainWindow, it will assign the value for "Name" property of person, that time PropertyChanged event is NULL.
If the same "person" class property "Name" assigned in Button_Click event, "PropertyChanged" event is NOT NULL and it is pointing to OnPropertyChanged.
My question is how "PropertyChanged" event is assigned to OnPropertyChanged method?
Thanks in advance.
The WPF data-binding infrastructure will add a PropertyChanged handler when you set the object as a DataContext, in order to detect changes to your properties.
You can watch this happen by setting a breakpoint.
The OnPropertyChanged method that it points to is an internal WPF method, as you can see by inspecting the Target property of the delegate.
The event will be null until something is subscribed to it. By the time the button click event has happened, it has a subscriber (via the databinding system).

Why my binding does not work?

Have class behine the xaml that contain the propertie 'Int32 Count'
I want to bind some TextBlock to the 'Count' value - that the TextBlock.Text will have the value of the 'Count'.
So i wrote in the xaml :
<TextBlock Text="{ Binding Path=Count }" />
And in the code behind the xaml i add to the constructor:
DataContext = this;
But each change of the 'Count' does not change the text of the TextBlock.
The code of 'Count'
Int32 count;
public Int32 Count
{
get
{
return count;
}
set
{
count = value;
}
}
place the INotifyPropertyChanged interface to your class:
public class MainPage : INotifyPropertyChanged
{
}
then implement:
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
what this does if provide the mechanism for notifying the view that something has changed in your datacontext and you do it like this:
public Int32 Count
{
get
{
return count;
}
set
{
count = value;
OnPropertyChanged("Count"); //This invokes the change
}
}
But of course, i recommend that you separate design and code using the MVVM pattern. This way, you can implement the propertychanged to a ViewModelBase class, then inherit that for each of your ViewModels.
You should ensure your DataContext implements INotifyPropertyChanged. Then you have to fire the property change events properly.

How can I trigger the PropertyChanged event from static method?

I have the following class
public class LanguagingBindingSource : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Dummy
{
get { return String.Empty; }
set
{
PropertyChanged(this, new PropertyChangedEventArgs("Dummy"));
}
}
}
that is bound to elements in XAML like this
Text="{Binding Dummy,Source={StaticResource languageSource},Converter={StaticResource languageConverter},ConverterParameter=labelColor}"
The sole purpose of the LanguageBindingSource class and its Dummy method is to allow property notifications to update the bindings when one or more resources change. The actual bound values are provided by the converter, looking up resources by the names passed as parameters. See the comments on this answer for more background.
My problem is that the resources are changed by a process external to the XAML pages containing the bindings and I need a single static method that I can call to trigger property change notification for all instances of the binding. I'm struggling to figure out just how I might do that. All ideas will be most appreciated.
Modify your class as follows:-
public class LanguagingBindingSource : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate {};
public static void FirePropertyChanged(string key)
{
((LanguagingBindingSource)Application.Resources[key]).NotifyPropertyChanged("Dummy");
}
private void NotifyPropertyChanged(string name)
{
PropertyChanged(this, new PropertyChangedEventArgs(name);
}
public string Dummy
{
get { return String.Empty; }
set
{
NotifyPropertyChanged("Dummy"));
}
}
}
Now are any point where you need to fire off this change use:-
LanguagingBindingSource.FirePropertyChanged("languageBindingSource");
Where "languageBindingSource" is the resource key that you are also using in your binding Source property.

Resources