英文:
C# Questions about the binding model structure
问题
以下是您提供的代码的中文翻译部分:
*DataModel 类
public DataModel()
{
this.MeasureA = new BindingList<MiddleMeasure>()
{
new MiddleMeasure()
{
}
};
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
*MiddleMeasure 类
public class MiddleMeasure
{
public BindingList<MeasureData> MeasureB
{
get; set;
} = new BindingList<MeasureData>();
public MiddleMeasure()
{
MeasureB.Add(new MeasureData()
{
Title = "500",
Title2 = 500,
Data = 10,
});
MeasureB.Add(new MeasureData()
{
Title = "501",
Title2 = 501,
Data = 20,
});
MeasureB.Add(new MeasureData()
{
Title = "503",
Title2 = 503,
Data = 30,
});
MeasureB.Add(new MeasureData()
{
Title = "504",
Title2 = 504,
Data = 40,
});
}
}
*MeasureData 类
public class MeasureData : INotifyPropertyChanged
{
private string m_Title;
private double m_Title2;
private double m_Data;
public MeasureData()
{
}
public MeasureData(double title, double data)
{
this.Title2 = title;
this.Title = title.ToString();
this.Data = data;
}
public string Title
{
get => this.m_Title;
set
{
this.m_Title = value;
OnPropertyChanged(nameof(Title));
}
}
public double Title2
{
get => this.m_Title2;
set
{
this.m_Title2 = value;
OnPropertyChanged(nameof(Title2));
}
}
public double Data
{
get => this.m_Data;
set
{
this.m_Data = value;
OnPropertyChanged(nameof(Data));
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
这是您提供的代码的中文翻译。如果您有任何其他问题或需要进一步的帮助,请随时告诉我。
英文:
*DataModel Class
public DataModel()
{
this.MeasureA = new BindingList<MiddleMeasure>()
{
new MiddleMeasure()
{
}
};
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
*MiddleMeasure Class
public class MiddleMeasure
{
public BindingList<MeasureData> MeasureB
{
get; set;
} = new BindingList<MeasureData>();
public MiddleMeasure()
{
MeasureB.Add(new MeasureData()
{
Title = "500",
Title2 = 500,
Data = 10,
});
MeasureB.Add(new MeasureData()
{
Title = "501",
Title2 = 501,
Data = 20,
});
MeasureB.Add(new MeasureData()
{
Title = "503",
Title2 = 503,
Data = 30,
});
MeasureB.Add(new MeasureData()
{
Title = "504",
Title2 = 504,
Data = 40,
});
}
}
-
MeasureData Class
public class MeasureData : INotifyPropertyChanged
{
private string m_Title;
private double m_Title2;
private double m_Data;public MeasureData() { } public MeasureData(double title, double data) { this.Title2 = title; this.Title = title.ToString(); this.Data = data; } public string Title { get => this.m_Title; set { this.m_Title = value; OnPropertyChanged(nameof(Title)); } } public double Title2 { get => this.m_Title2; set { this.m_Title2 = value; OnPropertyChanged(nameof(Title2)); } } public double Data { get => this.m_Data; set { this.m_Data = value; OnPropertyChanged(nameof(Data)); } } public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }
It is a structure with MiddleMeasure in DataModel and MeasureData class in it.
The class to bind to the itemsource is the MeasureData class.
this.xDataGrid.ItemsSource = this.m_Model.MeasureA;
If you try this, it won't bind.
Binding like this.m_Model.MeasureA[0].MeasureB is possible, but not what I want. All MeasureB in MeasureA must be bound.
how can i solve it?
答案1
得分: 0
Maybe you misunderstand the binding value in your code.
this.xDataGrid.ItemsSource = this.m_Model.MeasureA; If you try this, it won't bind.
这可能是你的代码中对绑定值的误解。
this.xDataGrid.ItemsSource = this.m_Model.MeasureA; 如果你尝试这样做,它不会绑定。
this.xDataGrid.ItemsSource = this.m_Model.MeasureA
: 这一行将ItemSource设置为BindingList<MiddleMeasure>()
,它只有一个项(MiddleMeasure)。就像 this.xDataGrid.ItemsSource = BindingList<MiddleMeasure>(){ new MiddleMeasure() }
一样。因此,ItemSource只包含一个项目。并且唯一的项目上只有一个BindingList<MeasureData>
属性。
类似 this.m_Model.MeasureA[0].MeasureB 的绑定是可能的,但不是我想要的。必须绑定 MeasureA 中的所有 MeasureB。
this.m_Model.MeasureA[0].MeasureB
的值是 BindingList<MeasureData>
。所以,当你将其设置为ItemSource时,绑定成功。
绑定到ItemSource的类是 MeasureData 类
这样,this.xDataGrid.ItemsSource
的值必须是 BindingList<MeasureData>
。但是你说必须绑定 MeasureA 中的所有 MeasureB。因此,你需要声明一个变量(类型为BindingList<MeasureData>
)来获取 MeasureA 的每个项的 MeasureB。
如果是这样的话,MiddleMeasure
类就没有意义了。你可以让 MeasureA 的类型为 BindingList<MeasureData>
。
英文:
Maybe you misunderstand the binding value in your code.
> this.xDataGrid.ItemsSource = this.m_Model.MeasureA; If you try this, it won't bind.
this.xDataGrid.ItemsSource = this.m_Model.MeasureA
: this line set the itemsource as the BindingList<MiddleMeasure>()
which just have one item(MiddleMeasure) in it. Just like the this.xDataGrid.ItemsSource = BindingList<MiddleMeasure>(){ new MiddleMeasure() }
. So the ItemSource just have one item. And there is only a BindingList<MeasureData>
property on the only item.
> Binding like this.m_Model.MeasureA[0].MeasureB is possible, but not what I want. All MeasureB in MeasureA must be bound
The value of the this.m_Model.MeasureA[0].MeasureB
is BindingList<MeasureData>
. So you bind successfully when you set it as the itemsource.
> The class to bind to the itemsource is the MeasureData class
The the value of the this.xDataGrid.ItemsSource
must be BindingList<MeasureData>
. But you said all MeasureB in MeasureA must be bound. So you need to declare a variable( type of BindingList<MeasureData>
) to get MeasureA's every item's MeasureB.
If so, the MiddleMeasure
class has no meanings.You can just let the MeasureA be type of BindingList<MeasureData>
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论