88647

Binding to a property within a static class instance

<strong>What I am trying to achieve</strong>

I have a WPF application (it's just for testing) and I want to bind the text (Content) of a label to a property somewhere. The idea is that this property value will be changed when the user chooses a different language. When the property changes, I want the label text to update with the new value.

<strong>What I have tried</strong>

I tried to create a static class with a static property for the label value. For example:

public static class Language { public static string Name = "Name"; }

I then was able to bind this value to my label using XAML like so:

Content="{Binding Source={x:Static lang:Language.Name}}"

And this worked fine for showing the initial value of "Name". The problem is, when the Name property changes the label value doesn't change.

So, back to the drawing board (Google). Then I found this answer which sounded exactly like what I needed. So here was my new attempt at this:

public class Language { public static Language Instance { get; private set; } static Language() { Instance = new Language(); } private Language() { } private string name = "Name"; public string Name { get { return name; } set { name = value; } } }

With my binding changed it this:

Content="{Binding Source={x:Static lang:Language.Instance}, Path=Name}"

This still results in the same problem.

<strong>Questions</strong>

What am I missing here? How can I get the label to update when the value is changed?

Answer1:

That simply isn't a property. Try:

public class Language { public static Language Instance { get; private set; } static Language() { Instance = new Language(); } private Language() { Name = "Name"; } public string Name {get;private set;} }

or with change notification:

public class Language : INotifyPropertyChanged { public static Language Instance { get; private set; } static Language() { Instance = new Language(); } private Language() { } private string name = "Name"; public string Name { get { return name; } set { SetValue(ref name, value);} } protected void SetValue<T>(ref T field, T value, [CallerMemberName]string propertyName=null) { if (!EqualityComparer<T>.Default.Equals(field, value)) { field = value; OnPropertyChanged(propertyName); } } protected virtual void OnPropertyChanged( [CallerMemberName]string propertyName=null) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; }

Recommend

  • How to convert WPF project so it can be used as a class library by a separate exe
  • Android Lock Screen C# .NET Replica
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • WP8.1 AppBarButton holding event
  • NUnit 3.0 TestCase const custom object arguments
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Zoom in and out of jPanel
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • Force show.bind execution
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Debug.DrawLine not showing in the GameView
  • Android activity accessing service's static reference before the service is ready
  • Switching to Release Build causes runtime error in Web Reference
  • Get one-time binding to work for ng-if
  • Display issues when we change from one jquery mobile page to another in firefox
  • Counter field in MS Access, how to generate?
  • sending/ receiving email in Java
  • Javascript + PHP Encryption with pidCrypt
  • Websockets service method fails during R startup
  • QuartzCore.framework for Mono Develop
  • Python - Map / Reduce - How do I read JSON specific field in using DISCO count words example
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • AT Commands to Send SMS not working in Windows 8.1
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper way to use connect-multiparty with express.js?
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • JTable with a ScrollPane misbehaving
  • coudnt use logback because of log4j
  • Java static initializers and reflection
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • apache spark aggregate function using min value
  • unknown Exception android
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?