【发布时间】:2016-09-30 19:41:12
【问题描述】:
我知道有一种方法可以将页面StackLayout 变量的数据绑定到类属性,但不幸的是我没有找到任何有关它的资源。我知道如何实现简单的绑定。例如:在 Editor 和 Label 之间。但是如何将Editor 输入文本绑定到类属性例如:string Number {get; set;}。这是我的课:
public class ThingsObject
{
public int Id { get; set; }
public bool IsStarted { get; set; }
public string Number {get; set; }
public string City { get; set; }
public Zones Zone { get; set; }
public ContentPage Page;
public DateTime StartDate { get; set; }
}
这是我的 PageLayout 类,其中包含 StackLayout 实现、变量和它们之间的绑定。
public partial class ThingPageLayout : CarouselPage
{
List<ThingsObject> things= new List<ThingsObject>();
public ThingPageLayout()
{
things.Add(new ThingsObject() { Id = 1, Number = null, City = null, Zone = null });
foreach (ThingsObject p in things)
{
Children.Add(CreatePage(p));
}
}
public ContentPage CreatePage(ThingsObject thing)
{
int uniqueId = this.IdPark;
var page = new ContentPage()
{
Content = new StackLayout { }
};
var label1 = new Label
{
Text = "Text",
TextColor = Color.Black,
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
HorizontalOptions = LayoutOptions.Center
};
((StackLayout)page.Content).Children.Add(label1);
var inputText = new Editor
{
TextColor = Color.Blue,
HorizontalOptions = LayoutOptions.Fill,
VerticalOptions = LayoutOptions.Fill
};
((StackLayout)page.Content).Children.Add(inputText);
// Binding here //
label1.BindingContext = inputText;
label1.SetBinding(Label.TextProperty, "Text");
// End of binding //
var activeButton = new Button
{
ClassId = uniqueId.ToString(),
Text = "Button",
HorizontalOptions = LayoutOptions.Fill,
VerticalOptions = LayoutOptions.Fill
};
((StackLayout)page.Content).Children.Add(activeButton);
thing.Page = page;
return page;
}
}
在这里,我想将现有对象 things Number 绑定到 Editor 插入值。因为当项目开始时,Number 的值是null,但是在Editor 输入之后我想将插入的文本绑定到我的类属性Number。
【问题讨论】:
标签: c# class data-binding xamarin xamarin.forms