[英]In Xamarin Forms, can I Bind to a property inside a list of the class I'm showing in listview?
I'm new to programming and I'm trying to develop an app using Xamarin Forms. 我是编程新手,正在尝试使用Xamarin Forms开发应用程序。 So far I was able to get most of the answers I needed from here or other tutorials, but this one was not possible.
到目前为止,我已经可以从这里或其他教程中获得所需的大多数答案,但是这是不可能的。
The idea - I want to show a list of users in a listview. 这个想法-我想在列表视图中显示用户列表。 It cell should show his "Name" and the "Value" of a "Topic".
该单元格应显示其“名称”和“主题”的“值”。 But as the user can teach more than one topic, he has a "List", like this:
但是由于用户可以教授多个主题,所以他有一个“列表”,如下所示:
public class User
{
public string Name { get; set; }
public List<Topic> Topics { get; set; }
public User()
{
Topics = new List<Topic>();
Topics.Add(new Topic { TopicName = "English", Value = 150.00 });
Topics.Add(new Topic { TopicName = "Portuguese", Value = 120.00 });
Topics.Add(new Topic { TopicName = "Latin", Value = 300.00 });
}
}
public class Topic
{
public string TopicName { get; set; }
public double Value { get; set; }
}
My App, just to show this trial is like this: 我的App只是为了显示此试用版,如下所示:
public App()
{
// The root page of your application
var template = new DataTemplate(typeof(CustomCell));
var users = new List<User>();
users.Add(new App1.User { Name = "Luizinho" });
users.Add(new App1.User { Name = "Zezinho" });
users.Add(new App1.User { Name = "Huguinho" });
var content = new ContentPage
{
Title = "StackFlow_01",
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
//new Label {Text = "funciona" }
new ListView {ItemTemplate = template, ItemsSource = users }
}
}
};
MainPage = new NavigationPage(content);
}
And finally the CustomCell: 最后是CustomCell:
public class CustomCell : ViewCell
{
public CustomCell()
{
//instantiate each of our views
StackLayout cellWrapper = new StackLayout();
StackLayout horizontalLayout = new StackLayout();
Label left = new Label();
Label right = new Label();
//set bindings
left.SetBinding(Label.TextProperty, "Name");
right.SetBinding(Label.TextProperty, "TopicName");
//add views to the view hierarchy
horizontalLayout.Children.Add(left);
horizontalLayout.Children.Add(right);
cellWrapper.Children.Add(horizontalLayout);
View = cellWrapper;
}
}
So, the Name binded to the Left label works fine (and most of the examples around to show something in the first "level" of the class), but how can I bind the Right label to the Value of Portuguese topic? 因此,将Name绑定到Left标签可以很好地工作(大多数示例在类的第一个“级别”中显示),但是如何将Right标签绑定到葡萄牙语主题的Value? Is it possible?
可能吗?
I know there should be also an IValueConverter, but the biding path is really what I'm not getting. 我知道也应该有一个IValueConverter,但是出价路径确实是我没有得到的。
您可以尝试绑定到列表中的第一个元素
right.SetBinding(Label.TextProperty, "Topics[0].TopicName");
Each user has own Topics, so you will need to decide what topic you want to bind to at some point unless you want to bind list of topics to another sub-view list. 每个用户都有自己的主题,因此除非需要将主题列表绑定到另一个子视图列表,否则您需要在某个时候决定要绑定到哪个主题。 You probably will need to derive from DataTemplateSelector to make such decision
您可能需要从DataTemplateSelector派生出来才能做出这样的决定
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.