2016-04-27 21 views
2

我有一個列表視圖,我將從我的數據庫獲得的值綁定到我的XAML。我將一個值綁定到我的XAML中,但是我希望綁定兩個值,這是可能的還是隻能在代碼中使用?如果是這樣,我將如何實現這一目標。無論如何,我可以將兩個值綁定到我的XAML中嗎?

這是我的代碼:

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? 
+0

我剛剛更新了我的答案,以更好地滿足您的問題 – Matt

回答

5

不幸的是,這還沒有(但?)支持。正如Pheonys所說,你可以在WPF中執行此操作,但不能在Xamarin.Forms中執行。

如果你想結合兩個特性在一個視圖模型,你應該創建另一個屬性是這樣的:

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}" /> 
+1

工程就像一個魅力。謝謝Matt – medvedo

1

你可以添加一個GET-唯一的財產,並綁定到這一點。

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

+0

我不能達到infoOne + infoTwo – medvedo

+1

你什麼意思,你不能「達到」呢? – AntiTcb

+0

當我將其添加到我的公開課時,它無法找到infoOne和infoTwo,我在它上面的值。 – medvedo

0

沒有內置的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> 
0

你試過以下?

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}" /> 
+0

是的,我試過了,找不到= infoOne +「」+ infoTwo不幸 – medvedo

+0

'infoOneAndinfoTwo'不應該有公共setter,或者被定義爲只讀字段。 – AntiTcb

+0

不知道我非常明白你的意思,我在上面展示了確切的代碼,當我嘗試連接infoOneAndinfoTwo = //在這裏找不到值時,我找不到infoOne + infoTwo。 – medvedo

相關問題