【问题标题】:C# wpf , partial ListView bindingC# wpf,部分 ListView 绑定
【发布时间】:2019-01-22 18:24:34
【问题描述】:

我有一个包含 100 个项目的列表,例如:

List<User> items = new List<User>();
            items.Add(new User() { Name = "John Doe", Age = 42, Mail = "john@doe-family.com" });
            items.Add(new User() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com" });
            items.Add(new User() { Name = "Sammy Doe", Age = 13, Mail = "sammy.doe@gmail.com" });
            lvDataBinding.ItemsSource = items;

我与 WPF 绑定

<ListView Margin="10" Name="lvDataBinding">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="Name: " />
                        <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                        <TextBlock Text=", " />
                        <TextBlock Text="Age: " />
                        <TextBlock Text="{Binding Age}" FontWeight="Bold" />
                        <TextBlock Text=" (" />
                        <TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
                        <TextBlock Text=")" />
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

如何仅绑定此列表的一部分?例如只有索引 3-7 中的项目?

我知道我可以创建一个新的列表,但是有什么聪明的方法吗?

【问题讨论】:

  • lvDataBinding.ItemsSource = items.Skip(3).Take(4); ?

标签: c# wpf listview binding


【解决方案1】:

这应该可行:

lvDataBinding.ItemsSource = items.GetRange(startIndex, count);

您还可以使用 LINQ 以不同方式过滤项目,例如:

lvDataBinding.ItemsSource = items.Where(item => item.Age > 20);
lvDataBinding.ItemsSource = items.Where(item => item.Name.Contains("J"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-20
    • 2011-08-19
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 2010-10-08
    • 2011-01-01
    • 2011-01-14
    相关资源
    最近更新 更多