[英]Xamarin.Forms Data Binding to class properties
I know that there are a way to bind data of page StackLayout
variables to class properties, but unfortunately I don't find any resources about it. 我知道有一种方法可以将页面
StackLayout
变量的数据绑定到类属性,但是不幸的是我找不到任何有关它的资源。 I know how to implement simple binding. 我知道如何实现简单的绑定。 For example: between
Editor
and Label
. 例如:在
Editor
和Label
之间。 But how can I bind Editor
input text to class property ex.: string Number {get; set;}
但是如何将
Editor
输入文本绑定到类属性ex .: string Number {get; set;}
string Number {get; set;}
. string Number {get; set;}
。 Here is my class: 这是我的课:
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; }
}
And here is my PageLayout class with StackLayout implementation , variables and binding between them. 这是我的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;
}
}
And here I would like to bind to my existing object things
Number
to Editor
inserted value. 在这里,我想绑定到我现有的对象
things
Number
to Editor
插入值。 Because when projects starts , Number
value is null
, but after Editor
input I want to bind inserted text to my class property Number
. 因为当项目开始时,
Number
值为null
,但是在Editor
输入之后,我想将插入的文本绑定到类属性Number
。
Basically every page (ContentPage, TabPage etc) has a property BindingContext
where you assign the object or better a ViewModel that you want to bind to. 基本上,每个页面(ContentPage,TabPage等)都具有
BindingContext
属性,您可以在其中分配对象,或者更好地将对象绑定到ViewModel。
So 所以
public class Page1 : ContentPage
{
public Page1()
{
BindingContext = new ThingsObject();
var lbl = new Label();
lbl.SetBinding(Label.TextProperty, "Number");
Content = new StackLayout
{
Children = {
lbl
}
};
}
}
The second parameter of the SetBinding()
method is a string that designates the target property of your binding context that you wish to bind to a property of the control. SetBinding()
方法的第二个参数是一个字符串,用于指定您希望绑定到控件属性的绑定上下文的目标属性。 The first parameter designates the property of the control that should be updated. 第一个参数指定应更新的控件的属性。
So I am pretty sure that in this line of code: 所以我很确定在这行代码中:
label1.SetBinding(Label.TextProperty, "Text");
You really intended to specific something other than "Text" for the second parameter, as I don't see a property named Text
declared on your ThingsObject
class. 您确实打算为第二个参数指定“文本”以外的其他内容,因为我没有在
ThingsObject
类上声明一个名为Text
的属性。
The Editor control also has a Text
property that you can bind. 编辑器控件还具有可以绑定的
Text
属性。 The code looks very similar, in this example which would establish a binding to your Number
property: 该代码看起来非常相似,在此示例中,该代码将建立对您的
Number
属性的绑定:
inputText.SetBinding(Editor.TextProperty, "Number");
You would also need to set the BindingContext
of the editor, but there is a trick you can use to save some code. 您还需要设置编辑器的
BindingContext
,但是有一个技巧可以用来保存一些代码。 If a control has bindings but does not have a BindingContext
explicitly set, it will look to its parent control (or its parent, and so on) until it finds a BindingContext value. 如果控件具有绑定但没有显式设置
BindingContext
,则它将查找其父控件(或其父控件,依此类推),直到找到BindingContext值为止。 So by changing this line of code in your example: 因此,通过在示例中更改以下代码行:
label1.BindingContext = inputText;
To this: 对此:
page.BindingContext = inputText;
Then you don't need to explicitly set the BindingContext
of any of the child controls, because they will inherit it from the parent page container. 然后,您无需显式设置任何子控件的
BindingContext
,因为它们将从父页面容器继承它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.