简体   繁体   English

无论如何,我可以将两个值绑定到我的XAML中吗?

[英]Is there anyway I can bind two values into my XAML?

I have a listview and I bind the values I get from my DB to my XAML. 我有一个列表视图,并将从数据库中获取的值绑定到XAML。 I bind one value now into my XAML but i wish to bind two values, is it possible or is it only possible in code? 我现在将一个值绑定到我的XAML中,但我希望绑定两个值,这是可能的还是仅在代码中可能? If so, how would I accomplish that. 如果是这样,我将如何实现。

This is my code: 这是我的代码:

    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: XAML:

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

Unfortunately this is not (yet?) supported. 不幸的是,尚不支持此功能。 As Pheonys says, you can do this in WPF but not in Xamarin.Forms. 正如Pheonys所说,您可以在WPF中执行此操作,但不能在Xamarin.Forms中执行此操作。

If you want to bind to two properties in one ViewModel you should create another property like this: 如果要绑定到一个ViewModel中的两个属性,则应创建另一个属性,如下所示:

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

Just change your item class to this. 只需将您的商品类别更改为此。

Your XAML will be like this: 您的XAML将如下所示:

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

您可以添加仅获取属性并绑定到该属性。

public string combinedInfo { get; } = $"{infoOne} {infoTwo}";

Making some assumptions (as I've not played in Xamarin), in WPF XAML you could use a multibinding class and a string formatter. 作一些假设(因为我没有在Xamarin中玩过),在WPF XAML中,您可以使用多重绑定类和字符串格式化程序。

MultiBinding Class (MSDN) 多绑定类(MSDN)

An example of doing this can be found in the answer to this stack overflow question: How to bind multiple values to a single WPF TextBlock? 在此堆栈溢出问题的答案中可以找到执行此操作的示例: 如何将多个值绑定到单个WPF TextBlock?

There is no built-in support for MultiBinding but you can use Xamarin.Forms.Proxy library to achieve it. 没有对MultiBinding的内置支持,但是您可以使用Xamarin.Forms.Proxy库来实现它。

    <Label.Text>
      <xfProxy:MultiBinding StringFormat="Good evening {0}. You are needed in the {1}">
        <Binding Path="User" />
        <Binding Path="Location" />
      </xfProxy:MultiBinding>
    </Label.Text>

have you tried the following? 您是否尝试过以下方法?

public class items
{
    public string infoOne { get; set;}
    public string infoTwo { get; set;}
    public string infoOneAndinfoTwo {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 (),
            infoOneAndinfoTwo = infoOne + " " + infoTwo
        });

   mylist.ItemsSource = theList;

 }

XAML: XAML:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM