Reputation: 2912
I am trying to binding the content of a label in xaml to a public variable in C# behind. When never the value gets set from the UI, it should trigger the value in the C#.
This VS2008 C# + WPF
However, it does not compile, and it says:
error CS1502: The best overloaded method match for
'System.Windows.DependencyObject.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs)' has some invalid arguments
error CS1503: Argument '1': cannot convert from 'string' to 'System.Windows.DependencyPropertyChangedEventArgs'
Here is some code
C# code
private bool _isLogOn;
public bool IjmFinished
{
get
{
// return _fl.ProcessFinished;
return _isLogOn;
}
set
{
_isLogOn = value;
OnPropertyChanged("IjmFinished");
}
}
xaml
<Label Margin="120,0,0,82" Height="30" VerticalAlignment="Bottom" Content="{Binding Path = IjmFinished, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IjmFinishedStatusConverter}}" HorizontalAlignment="Left" Width="80" />
If I uncomment out OnPropertyChanged("IjmFinished");
it can compile. I am wondering where I did wrong, and how I should change it? Thanks.
Upvotes: 3
Views: 3231
Reputation: 102793
It looks like you're calling a method in your class that is meant to handle a Dependency Property change. Instead, you need to raise the INotifyPropertyChanged.PropertyChanged
event:
PropertyChanged(this, new PropertyChangedEventArgs("ImFinished"));
Note that typically you will write a helper method for this in your base view-model class, which looks something like this:
protected void RaisePropertyChanged(string propertyName)
{
var evt = PropertyChanged; // create local copy in case the reference is replaced
if (evt != null) // check if there are any subscribers
evt (this, new PropertyChangedEventArgs(propertyName));
}
That way you can use simply RaisePropertyChanged("ImFinished")
.
One final note: C# version 5 has a new attribute CallerMemberName
which allows the RaisePropertyChanged
method to get the calling property's name:
protected void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
var evt = PropertyChanged; // create local copy in case the reference is replaced
if (evt != null) // check if there are any subscribers
evt (this, new PropertyChangedEventArgs(propertyName));
}
This saves you from the use of magic strings to specify the property name -- just call RaisePropertyChanged()
from the property setter.
Upvotes: 4