I am trying to bind a static property of a different class to the Text Property of a TextBlock and can get the binding to work, but there's is no update to the Text Property when the static property's value has changed. I have read that I cannot use INotifyPropertyChanged because the property is static and have seen a number of solutions that suggest to use a Dependency Property. I am very new to C# and do not really understand how to use Dependency Properties, but have made a couple of attempts which do not seem to work for two reasons. 1. My static property has custom getter and setter and 2. The static property is used in a number of static methods which I can't figure out how to make work using a Dependency Property. I do not know how to use a custom getter and setter when using a Dependency Property or if this can even be done or how to continue using the static property in static methods after I change it to a Dependency Property.
Here is the current code for the static property:
public class Helper
{
public static string Token
{
get
{
using (StreamReader streamReader = new StreamReader("Token.ini"))
{
return streamReader.ReadLine();
}
}
set
{
using (StreamWriter streamWriter = new StreamWriter("Token.ini"))
{
streamWriter.WriteLine(value);
}
}
}
public static MethodThatUsesToken(){}
public static OtherMethodThatUsesToken(){}
And here the current XAML for the binding which works but doesn't update:
<Window.Resources>
<local:Helper x:Key="helper"/>
</Window.Resources>
<TextBlock Text="{Binding Source={StaticResource helper},Path=Token Converter={StaticResource NameConverter}}"/>
I really appreciate any help!