2

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?
medvedo
  • 543
  • 2
  • 8
  • 23

5 Answers5

5

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}" />
Matt
  • 4,612
  • 1
  • 24
  • 44
0

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

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

AntiTcb
  • 609
  • 4
  • 15
0

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?

Community
  • 1
  • 1
Pheonyx
  • 851
  • 6
  • 15
0

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>
Giorgi
  • 30,270
  • 13
  • 89
  • 125
0

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}" /> 
CharlesMighty
  • 572
  • 1
  • 5
  • 15
  • Yeah I tried it, cannot find = infoOne + " " + infoTwo unfortantly – medvedo Apr 27 '16 at 18:28
  • `infoOneAndinfoTwo` should not have a public setter, or be defined as a readonly field. – AntiTcb Apr 27 '16 at 18:33
  • Not sure I quite understand what you mean, I have the exact code as you showcase above and I cannot find infoOne + infoTwo when I try to connect it wiht the infoOneAndinfoTwo = //cannot find the values here. – medvedo Apr 27 '16 at 18:36