[英]How can I bind a List<T> to relevant text boxes on a form?
I have the following classes:我有以下课程:
public class TemplateTestLine
{
public int Id { get; set; }
public string Description { get; set; }
public string Hyperlink { get; set; }
public int NumOfReadings { get; set; }
public UnitOfMeasure UnitOfMeasure { get; set; } = new UnitOfMeasure();
public MethodOfInput MethodOfInput { get; set; } = new MethodOfInput();
public bool Alarm { get; set; }
public bool Signature { get; set; }
public List<TemplateReading> Readings { get; set; } = new List<TemplateReading>();
}
public class TemplateReading
{
public int Id { get; set; }
public int TestLineId { get; set; }
public int ReadingTypeId { get; set; }
public string Value { get; set; }
}
In my database I have a number of reading types, which relate to the ReadingTypeId
like so...在我的数据库中,我有许多阅读类型,它们与
ReadingTypeId
相关,就像这样......
Id Description
----------------
1 Distance Between Readings
2 Readings Target Min
3 Readings Target Max
4 Max Difference Between Readings
5 Tolerance Between Jumps
6 Straightness Max Difference Between Readings
7 Straightness Tolerance Between Jumps
8 Straightness Target Min
9 Straightness Target Max
10 Readings Tolerance Min
11 Readings Tolerance Max
12 Straightness Tolerance Min
13 Straightness Tolerance Max
To create or edit a TemplateTestLine
I have a form with inputs(mainly text boxes) that I want to bind to the TemplateTestLine
object that is passed to the form.要创建或编辑
TemplateTestLine
,我有一个带有输入(主要是文本框)的表单,我想将其绑定到传递给表单的TemplateTestLine
对象。 So, 7 for each of the main properties (which are easy enough to bind using the 'Advanced DataBinding Editor'), plus 13 text boxes for the List<TemplateReading>
like so...因此,每个主要属性 7 个(使用“高级数据绑定编辑器”很容易绑定),加上
List<TemplateReading>
的 13 个文本框,就像这样......
So my question is....所以我的问题是......
How can I bind the readings text boxes on the form so that when I pass a TemplateTestLine
to the form, the List<TemplateReading>
populate their relevant ReadingTypeId
text boxes with the Value
property?如何绑定表单上的读数文本框,以便当我将
TemplateTestLine
传递给表单时, List<TemplateReading>
使用Value
属性填充其相关的ReadingTypeId
文本框?
Can it be done from the 'Advanced Binding Editor' or will I need to forget that and come up with a coded solution?可以从“高级绑定编辑器”完成,还是我需要忘记这一点并提出编码解决方案?
Thanks in advance.提前致谢。
To close this question, @Jimi's solution in the comments worked great:为了结束这个问题,@Jimi 在评论中的解决方案效果很好:
"It's simple enough to bind a Property of one of the objects in the list to the Text Property of the related TextBox" “将列表中对象之一的属性绑定到相关文本框的文本属性很简单”
[TextBox].DataBindings.Add("Text", [TemplateTestLine Instance].Readings.Single(r => r.ReadingTypeId == 1), "Value", false, DataSourceUpdateMode.OnPropertyChanged);
Thanks谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.