med*_*edo 2 c# xamarin xamarin.forms
我有一个listview,我将从数据库中获取的值绑定到我的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;
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<Label Text = "{Binding infoOne}" /> //How could I add infoTwo to the same label and also add a space between them?
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不是(还是?)支持.正如Pheonys所说,你可以在WPF中执行此操作,但不能在Xamarin.Forms中执行此操作.
如果要绑定到一个ViewModel中的两个属性,则应创建另一个属性,如下所示:
public class items
{
public string infoOne { get; set;}
public string infoTwo { get; set;}
public string infoFull
{
get { return $"{infoOne} {infoTwo}"; }
}
}
Run Code Online (Sandbox Code Playgroud)
只需将您的项目类更改为此.
你的XAML将是这样的:
<Label Text = "{Binding infoFull}" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1364 次 |
| 最近记录: |