我有一个 ListView ,并将从数据库获取的值绑定(bind)到 XAML。我现在将一个值绑定(bind)到我的 XAML 中,但我希望绑定(bind)两个值,是否可能或仅在代码中可能?如果是这样,我将如何实现这一目标。
这是我的代码:
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?
最佳答案
不幸的是,这(还?)不支持。正如 Pheonys 所说,您可以在 WPF 中执行此操作,但不能在 Xamarin.Forms 中执行此操作。
如果您想绑定(bind)到一个 ViewModel 中的两个属性,您应该创建另一个属性,如下所示:
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}" />
关于c# - 无论如何,我可以将两个值绑定(bind)到我的 XAML 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36897370/