I have an App that groups all the global variables in a single class. They are used over MANY classes in the App. (300+ variables, shortened for demo):
public class Vars
{
public static string DateStr = "";
}
I want to use x:Bind to One-way bind the data to fields on a page:
<TextBlock x:Name="Local_Data" Text="{x:Bind local:Vars.DateStr, Mode=OneWay}" Style="{StaticResource TextBlockStyle1}"/>
OneTime binding seems to work OK. I can refresh the page and the DateStr reflects the new value.
I changed the Vars class definition to:
public class Vars : INotifyPropertyChanged
{
private static string _DateStr = "hello";
public static string DateStr
{
get { return _DateStr; }
set
{
_DateStr = value;
OnPropertyChanged();
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises this object's PropertyChanged event.
/// </summary>
/// <param name="propertyName">The property that has a new value.</param>
protected void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
#endregion
}
When I try to build, I get the message:
An object reference is required for the non-static field, method, or property 'Vars.OnPropertyChanged(string)'
If I change it to:
protected static void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
I get the message:
Keyword 'this' is not valid in a static property, static method, or static field initializer
I assume that it is something trivial that I am missing. What is the proper way to flag the data changing?
Thanks for your help. Dan
As the error message mentioned, the reason for your issue is that you are using Static
modifier for the binding source property- Vars.DateStr
. You just need to remove the Static
modifier of the Vars.DateStr
property.
public string DateStr
{
get { return _DateStr; }
set
{
_DateStr = value;
OnPropertyChanged("DateStr");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.