【问题标题】:Bind more than one property to a Xamarin ListView将多个属性绑定到 Xamarin ListView
【发布时间】:2018-11-29 20:28:57
【问题描述】:

有没有一种方法可以为 ListView 内的多个属性添加多个绑定? 我目前在我的 xaml 中有这段代码:

<ListView x:Name="ItemsListView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding .}"></Label>
                            <Button Text="{Binding Button}"></Button>
                        </StackLayout>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

现在我可以用这个 C# 代码绑定标签内的文本:

ItemsListView.ItemsSource = names; //names being a string array

上面的代码适用于标签,但我似乎不知道如何绑定按钮文本。 帮助将不胜感激

【问题讨论】:

  • ItemSource 应该绑定到对象集合。然后每个控件、标签、按钮等都可以绑定到该对象上的不同属性。
  • 可以参考 Wasif Mahmood Mustafa 的回答。绑定模型到ItemsSource

标签: c# listview xamarin


【解决方案1】:

给你。

 <ListView x:Name="ItemsListView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <StackLayout Orientation="Horizontal">
                                <Label Text="{Binding Name}"></Label>
                                <Button Text="{Binding ButtonName}"></Button>
                            </StackLayout>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

然后创建一个包含这两个属性的类。

public class ListClass
{
    public string Name { get; set; }
    public string ButtonName { get; set; }
}

在您的页面中初始化您的列表。

List<ListClass> lists = new List<ListClass>()
        {
            new ListClass(){ Name = "John" , ButtonName = "Submit"},
            new ListClass(){ Name = "Alexa" , ButtonName = "Click"}
        };

最后将其绑定到 ItemsSource。

ItemsListView.ItemsSource = lists;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 2020-11-07
    • 2018-10-24
    • 2018-09-09
    • 1970-01-01
    • 2018-02-12
    相关资源
    最近更新 更多