[英]C# binding received data
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). 客户端应用程序具有GUI(带有MVVM模式的WPF)。 Client receives data from server every 1 second and they must be displayed in window. 客户端每隔1秒钟从服务器接收一次数据,这些数据必须显示在窗口中。 Screenshot (click) 屏幕截图(点击)
If I want it, I must RaisePropertyChanged()
, but I don't want to interfere in DataSctructure
class and make properties with RaisePropertyChanged()
. 如果需要的话,我必须使用RaisePropertyChanged()
,但是我不想干扰DataSctructure
类并使用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. 我可以在ViewModel中创建很多属性(例如AxisX
, AxisY
,...),并从ListOfValues
分配数据,但是我认为这是不合理的。
Or, may be, I must change structure of application? 或者,可能是我必须更改应用程序的结构?
UPDATE 1: 更新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; }
}
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
, ie with a getter at least), you already have a collection (a list) bindable to a (read only) Control (a DataGrid
). 然后,如果Value是具有属性的类(例如IntValue
和PhysValue
,即至少具有吸气剂),则您已经具有可绑定到(只读)控件( 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 当然,您必须详细说明才能实现所需的屏幕截图
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
. 您可以将每个Value
包装在视图模型的属性中,然后在其中调用PropertyChanged
,也可以修改DataStructure
类以触发PropertyChanged
。 You will have to make DataStructure
non static in order to implement INotifyPropertyChanged on it, however. 但是,您必须使DataStructure
非静态的,才能对其实施INotifyPropertyChanged。
Speaking of which, why are you storing mutable data in a static class? 说到哪个,为什么要在静态类中存储可变数据? That is a very bad idea... 那是一个非常糟糕的主意...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.