简体   繁体   English

在Xamarin Forms中,我可以绑定到要在列表视图中显示的类的列表中的属性吗?

[英]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.

相关问题 如何在 Xamarin.Forms 中的 ListView 中绑定列表 - How to bind list inside ListView in Xamarin.Forms 绑定列表视图Xamarin表单中的自定义项目列表 - bind custom list of items in a listview Xamarin forms Xamarin Forms-如何在ListView中显示/绑定列表? - Xamarin Forms - how to display/Bind List in ListView? 如何在Xamarin Forms中的另一个列表中获取列表并将其绑定到ListView中? - How to fetch list inside of another list and bind it inside ListView in Xamarin Forms? 如何在ScrollView的ScrollToAsync属性中将索引值绑定到元素参数-Xamarin Forms - How can I bind index value to element param in ScrollToAsync property of ScrollView - xamarin forms 使用ListView时,如何在Xamarin Forms中将自定义控件绑定设置为父视图模型属性? - How can I set custom control binding to parent view model property in Xamarin Forms when using a ListView? 在 Xamarin.Forms 中,如何绑定到自定义类列表、ListView 和打印自定义类属性? - In Xamarin.Forms, how to Bind to list of custom class, for ListView and print custom class properties? 我可以以编程方式设置 Xamarin.Forms ListView 中按钮的颜色吗? - Can I set the color of a button inside a Xamarin.Forms ListView programmatically? 创建列表后,如何更新Xamarin Forms ListView的ViewCell属性? - How can I update a Xamarin Forms ListView's ViewCell properties after the list has been created? Xamarin表单-如何在代码中“绑定”字符串? - Xamarin Forms - How can I “Bind” a string in the code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM