[英]How to Binding via C# code, not Binding via xaml?
In xaml, value = {binding viewmodelValue}
can be passed, but I don't know how to implement it in C#. xaml中可以传
value = {binding viewmodelValue}
,但不知道C#中如何实现。
You need to set the binding context to the ViewModel.您需要将绑定上下文设置为 ViewModel。 In your view backend you can set
在您的视图后端中,您可以设置
public partial class MySampleBackendPage : ContentPage
{
public MySampleBackendPage()
{
InitializeComponent();
this.BindingContext = new ViewModel();
}
}
This would bind your context to the view model.这会将您的上下文绑定到视图 model。
Say your ViewModel class looks like this:假设您的 ViewModel class 看起来像这样:
public class ViewModel
{
List<string> TextBoxValues = new List<string>();
}
Now You can bind it in xaml with the following:现在您可以将其绑定到 xaml 中,方法如下:
<ListView x:Name="EmployeeView"
ItemsSource="{Binding TextBoxValues}">
<ListView.ItemTemplate>
<DataTemplate>
<Entry Text={Binding} BindingMode="TwoWay"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.