medvedo
medvedo

Reputation: 563

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. 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? 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:

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

Upvotes: 2

Views: 2886

Answers (5)

Matt
Matt

Reputation: 4603

Unfortunately this is not (yet?) supported. As Pheonys says, you can do this in WPF but not in Xamarin.Forms.

If you want to bind to two properties in one ViewModel you should create another property like this:

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:

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

Upvotes: 5

CharlesMighty
CharlesMighty

Reputation: 572

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:

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

Upvotes: 0

Giorgi
Giorgi

Reputation: 30873

There is no built-in support for MultiBinding but you can use Xamarin.Forms.Proxy library to achieve it.

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

Upvotes: 0

AntiTcb
AntiTcb

Reputation: 649

You could add a get-only property and bind to that.

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

Upvotes: 0

Pheonyx
Pheonyx

Reputation: 851

Making some assumptions (as I've not played in Xamarin), in WPF XAML you could use a multibinding class and a string formatter.

MultiBinding Class (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?

Upvotes: 0

Related Questions