c# - 无论如何,我可以将两个值绑定(bind)到我的 XAML 中吗?

标签 c# xamarin xamarin.forms

我有一个 ListView ,并将从数据库获取的值绑定(bind)到 XAML。我现在将一个值绑定(bind)到我的 XAML 中,但我希望绑定(bind)两个值,是否可能或仅在代码中可能?如果是这样,我将如何实现这一目标。

这是我的代码:

    public class items
    {
        public string infoOne { get; set;}
        public string infoTwo { get; set;}
    }

    async void loadList ()
    {

        var getItems = await phpApi.getEvents ();


        theList = new List <items> ();
        foreach (var currentitem in getItems["results"]) {

            theList.Add (new items () {

                infoOne = currentitem ["name"].ToString (), 
                infoTwo = currentitem ["phone"].ToString ()
            });

       mylist.ItemsSource = theList;

     }

XAML:

        <Label Text = "{Binding infoOne}" /> //How could I add infoTwo to the same label and also add a space between them?

最佳答案

不幸的是,这(还?)不支持。正如 Pheonys 所说,您可以在 WPF 中执行此操作,但不能在 Xamarin.Forms 中执行此操作。

如果您想绑定(bind)到一个 ViewModel 中的两个属性,您应该创建另一个属性,如下所示:

public class items
{
    public string infoOne { get; set;}
    public string infoTwo { get; set;}
    public string infoFull
    {
        get { return $"{infoOne} {infoTwo}"; }
    }
}

只需将您的项目类别更改为此即可。

您的 XAML 将如下所示:

<Label Text = "{Binding infoFull}" />

关于c# - 无论如何,我可以将两个值绑定(bind)到我的 XAML 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36897370/

相关文章:

Xamarin 表单从 View 模型绑定(bind)命令到 ListView 数据模板中的按钮

c# - 如何在C#中实现MATLAB 'tansig'双曲正切sigmoid传递函数

c# - 更改 C# 字典中项目的基数

xamarin.ios - 无法连接到 Mac Build Host -- 加载 xcode 信息时出错

c# - 无法将位图转换为字节 []

carousel - Xamarin.Forms 轮播中的页面指示器,可能吗?

c# - View 根据 child 数量扩展自己的宽度

c# - 在 Entity Framework where 子句中比较字节数组

c# - 当文件名有点 "free form"时,有没有办法将文件名一致地转换为日期时间值?

android - 如何移动随 Visual Studio 2017 安装的 Android SDK 文件夹?