0

I have a lot of static poperties in my class library. I want to bind the property values in grid with two way binding. How can bind it?

public class AllStaticProperty
{
    public static int JA{get;set;}
    public static float JB{get;set;}
    public static bool JC{get;set;}
    public static int[] JD=new int[1000];
    //More properties here
    public static float[] ZZ=new float[2000];
}

I want bind static property name grid first column field and user enter the property value in grid second column then back to store the value in static property. how can I bind( I have more than 3000 static property)

thatguy
  • 21,059
  • 6
  • 30
  • 40
CoderGreat
  • 11
  • 2

1 Answers1

0

If you want to bind properties and reflect changes to them in the user interface, you have to implement INotifyPropertyChanged in the corresponding class and raise the PropertyChanged event whenever a property changes its value to trigger binding updates in controls to get the latest value.

However, static properties cannot access instance methods, so how should they raise property changed notifications? There are ways to achieve this, as you can see in this related post, but it is bad design. Another issue here is to bind a static property of a non-static class two-ways, but there are also workarounds.

I recommend to overthink your design and create view models that implement INotifyPropertyChanged, e.g.:

public class SampleViewModel : INotifyPropertyChanged
{
   private int _ja;
   public int JA
   {
      get => _ja;
      set
      {
         if (_ja == value)
            return;

         _ja = value;
         OnPropertyChanged();
      }
   }

   // ...other properties and backing fields.

   public event PropertyChangedEventHandler PropertyChanged;

   protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
   {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }
}

Then you can bind a property with the usual syntax and add Mode=TwoWay if that is not the default already.

SomeProperty="{Binding JA, Mode=TwoWay}"

An alternative option is to create one or more wrapper view models that are implemented as above, but access the static properties of your AllStaticProperty class, but then you need to synchronize the view model with the static properties, too, if any static property changes.

thatguy
  • 21,059
  • 6
  • 30
  • 40