C# 代码中的数据绑定控件(.cs 文件) [英] Data binding controls in C# code (.cs file)

查看:32
本文介绍了C# 代码中的数据绑定控件(.cs 文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找数据绑定,但不是在 xaml 中,而是在 .cs 文件中,是否有一种方式(属性)允许我将特定属性绑定到变量?

i have been searching for data binding but instead of being in xaml i am searching for being in .cs file, is there a manner (property) that allows me to bind the specific property to a variable?

我想绑定Entry"控件的Text"属性,但问题是我该怎么做?

I want to bind the "Text" property of the "Entry" control but the problem is that how do i do it?

这是我尝试过的,但我不确定,因为显然代码不正确

this is what i have tried, but i'm not sure about it beacuse OBVIOUSLY the code wasn't correct

textBox.SetBinding(Entry.TextProperty, 
      "MainViewModel.GetInstance().SubtasksList[TaskNumber - 1]");

代码:

var textBox = new Entry()
{
           Placeholder = "ex Math homework",
   TextColor = Xamarin.Forms.Color.FromHex("#18E1BF"),
   HorizontalOptions = LayoutOptions.Center,
   VerticalOptions = LayoutOptions.Center,
   Text = MainViewModel.GetInstance().SubtasksList[TaskNumber - 1]
};

textBox.SetBinding(Entry.TextProperty, 
      "MainViewModel.GetInstance().SubtasksList[TaskNumber - 1]");

当我想从属性(列表索引)中获取数据时,它没有被更改,那为什么?

when i want to take the data from the property (list index) just it didnt get changed, so why?

换句话说,我想做的是:

In other words, what I want to do is this:

<Entry
               Text="{Binding EntryText}"
               FontAttributes="Bold"
               FontSize="Large"
               HorizontalOptions="Center"
               VerticalOptions="CenterAndExpand" />

但不要使用 XAML 文件,而是使用 .cs 文件我是怎么做的?

But instead of XAML file, use .cs file How I do it?

推荐答案

如果您希望将一个元素(基于索引)从 C# 集合绑定到一个 Entry,您可以这样做.

If you are looking to bind an Element (index-based) from a C# Collection to an Entry you can do it this way.

获取您的 ViewModel 实例并将其设置为页面上的 BindingContext

Get your ViewModel instance and set it as the BindingContext on the Page

var viewModel = MainPageViewModel.GetInstace();
BindingContext = viewModel;

如果您有多个条目,请创建您的条目:

Create your entries in case you have more than one:

var textBox1 = new Entry()
{
    Placeholder = "ex Math homework",
    TextColor = Xamarin.Forms.Color.FromHex("#18E1BF"),
    HorizontalOptions = LayoutOptions.Center,
    VerticalOptions = LayoutOptions.Center
};

var textBox2 = new Entry()
{
    Placeholder = "ex Science homework",
    TextColor = Xamarin.Forms.Color.FromHex("#18E1BF"),
    HorizontalOptions = LayoutOptions.Center,
    VerticalOptions = LayoutOptions.Center
};

var textBox3 = new Entry()
{
    Placeholder = "ex English homework",
    TextColor = Xamarin.Forms.Color.FromHex("#18E1BF"),
    HorizontalOptions = LayoutOptions.Center,
    VerticalOptions = LayoutOptions.Center
};

设置绑定.对于这个例子,因为我没有得到 TaskNumber 的意思,所以我使用的是固定索引

Set your bindings. For this example, since I didn't get what TaskNumber meant I am using fixed indexes instead

textBox1.SetBinding(Entry.TextProperty, "SubtasksList[0]");
textBox2.SetBinding(Entry.TextProperty, "SubtasksList[1]");
textBox3.SetBinding(Entry.TextProperty, "SubtasksList[2]");

然后您只需将您的条目添加到您的视图中.我正在使用一个 StackLayout,我称之为 ItemsContainer

Then you just add your Entries to your View. I am using a StackLayout which I called ItemsContainer

ItemsContainer.Children.Add(textBox1);
ItemsContainer.Children.Add(textBox2);
ItemsContainer.Children.Add(textBox3);

上面的结果应该是这样的:

The above should give you a result like this:

一个 Xamarin 页面,其中包含 3 个以编程方式创建的 Entries,每个 Entries 都绑定到 C# 集合的一个项目.

A Xamarin Page with 3 programmatically created Entries each one bound to an item of a C# Collection.

希望这会有所帮助.-

这篇关于C# 代码中的数据绑定控件(.cs 文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆