C# binding received data

2019-09-12 09:53发布

I have got client-server app, where client gets data from server and saves it in static class:

 public static class DataStructure
    {
        //! Значение каждой переменной
        public static Value AxisX = new Value(@"axis_x", Dividers.AxisX);
        public static Value AxisY = new Value(@"axis_y", Dividers.AxisY);
        public static Value AxisZ = new Value(@"axis_z", Dividers.AxisZ);
        public static Value Temp = new Value(@"temp", Dividers.Temp);
        public static Value GirX = new Value(@"gir_x", Dividers.GirX);
        public static Value GirY = new Value(@"gir_y", Dividers.GirY);
        public static Value GirZ = new Value(@"gir_z", Dividers.GirZ);
        ...

        public static List<Value> ListOfValues = new List<Value>
        {
            AxisX,
            AxisY,
            AxisZ,
            Temp,
            GirX,
            GirY,
            GirZ,
            ....
        }
        ...

Client-side app has GUI (WPF with MVVM pattern). Client receives data from server every 1 second and they must be displayed in window. Screenshot (click)

If I want it, I must RaisePropertyChanged(), but I don't want to interfere in DataSctructure class and make properties with RaisePropertyChanged(). What is the best way to do it? I can create a lot of properties in ViewModel (such as AxisX, AxisY, ...) and assign them data from ListOfValues, but I think it is irrationally.

Or, may be, I must change structure of application?

UPDATE 1:

    public class Value
        {
            public Value(string name, double divider = 1.0)
            {
                Name = name;
                Divider = divider;
                HexCode = string.Empty;
                IntValue = 1;
                PhysValue = 1.0;
            }

            public readonly string Name;
            public readonly double Divider;
            public string HexCode { get; private set; }
            public int IntValue { get; private set; }
            public double PhysValue { get; private set; }
}

标签: c# wpf mvvm
2条回答
Emotional °昔
2楼-- · 2019-09-12 10:37

You can either wrap each Value in a property on your viewmodel and call PropertyChanged there, or you can modify the DataStructure class to fire PropertyChanged. You will have to make DataStructure non static in order to implement INotifyPropertyChanged on it, however.

Speaking of which, why are you storing mutable data in a static class? That is a very bad idea...

查看更多
爷的心禁止访问
3楼-- · 2019-09-12 10:54

Look for instance at my answer here: in this case

public class Value: INotifyPropertyChanged
    {
       public event PropertyChangedEventHandler PropertyChanged;

       protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
       {
          //this.VerifyPropertyName(propertyName);

          PropertyChangedEventHandler handler = this.PropertyChanged;
          if (handler != null)
          {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
           }
        }

Then if Value is a class with properties (like IntValue and PhysValue, i.e. with a getter at least), you already have a collection (a list) bindable to a (read only) Control (a DataGrid).

for example

private double physValue;        
public double PhysValue { 
  get { return physValue; } 
  set {
     physValue = value;
     RaisePropertyChanged();
  }
}

Of course you have to elaborate more to implement your desired screenshot

查看更多
登录 后发表回答