Binding(四)——使用集合对象作为列表控件的ItemsSource

本文介绍如何在WPF中利用ListBox结合Binding显示自定义对象集合,并通过DataTemplate自定义每项的显示样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WPF中的列表式控件派生自ItemsControl类,自然也就继承了ItemsSource这个属性。ItemsSource属性可以接受一个IEnumerable接口派生类的实例作为自己的值。

每个ItemsControl的派生类都具有自己对应的条目容器。例如ListBox的条目容器是ListBoxItem,ComboBox的条目容器时ComboBoxItem。ItemsSource里存放的是一条一条数据,要想把数据显示出来需要为它们穿上“外衣”,条目容器就起到了数据外衣的作用。怎样让条目容器与它对应的数据条目关联起来呢?当然是依靠Binding!

UI代码如下:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Binding Source" Height="240" Width="300">
    <StackPanel x:Name="stackPanel" Background="LightBlue">
        <TextBlock Text="StudentID:" FontWeight="Bold" Margin="5"/>
        <TextBox x:Name="textBoxId" Margin="5"/>
        <TextBlock Text="Student List:" FontWeight="Bold" Margin="5"/>
        <ListBox x:Name="listBoxStudents" Height="110" Margin="5"/>
    </StackPanel>
</Window>

我们要实现的效果是把一个List<Student>集合的实例作为ListBox的ItemsSource,让ListBox显示Student的Name并使用TextBox显示ListBox当前选中条目的Id。逻辑层代码:

        public MainWindow()
        {
            InitializeComponent();

            List<Student> stuList = new List<Student>()
            {
                new Student(){Id=0,Name="Tim",Age=29},
                new Student(){Id=1,Name="Tom",Age=28},
                new Student(){Id=2,Name="Kyle",Age=27},
                new Student(){Id=3,Name="Tony",Age=26},
                new Student(){Id=4,Name="Vina",Age=25},
                new Student(){Id=5,Name="Mike",Age=24},
            };

            this.listBoxStudents.ItemsSource = stuList;

            Binding binding = new Binding("SelectedItem.Id") { Source = this.listBoxStudents };
            this.textBoxId.SetBinding(TextBox.TextProperty, binding);
        }

当DisplayMemberPath属性被设置后,ListBox在获得ItemsSource的时候就会创建等量的ListBoxItem并以DisplayMemberPath属性值为Path创建Binding,Binding的目标是ListBoxItem的内容插件。

事实上在为ListBox设置Binding时,隐式生成一个DataTemplate类型(就仿佛给数据穿上了一件衣服一样)。至于什么是DataTemplate讲会放到Template相关章节去讨论。

最后,我们看一个为数据显示设置DataTemplate的例子。先删除C#中的this.listBoxStudents.DisplayMemberPath = "Name";再在XAML中添加几行代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Binding Source" Height="240" Width="300">
    <StackPanel x:Name="stackPanel" Background="LightBlue">
        <TextBlock Text="StudentID:" FontWeight="Bold" Margin="5"/>
        <TextBox x:Name="textBoxId" Margin="5"/>
        <TextBlock Text="Student List:" FontWeight="Bold" Margin="5"/>
        <ListBox x:Name="listBoxStudents" Height="120" Margin="5">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBox Text="{Binding Path=Id}" Width="30"/>
                        <TextBox Text="{Binding Path=Name}" Width="60"/>
                        <TextBox Text="{Binding Path=Age}" Width="30"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Window>

最后效果:


最后特别提醒一点:在使用集合类型作为列表控件的ItemsSource时一般会考虑使用ObservableCollection<T>代替List<T>,因为OvservableCollection<T>类实现了INotifyCollectionChanged和INotifyPropertyChanged接口,能把集合的变化立刻通知显示它的列表控件,改变会立刻显现出来。

在 Xamarin 中,ListView 是一个非常常见的控件,用于显示一个竖直滚动的列表。但如果我们需要实现一个横向滚动的列表,该怎么办呢?这时候,我们可以使用自定义的 ItemsControl 来实现这个功能。 ItemsControl 是一个用于显示一个集合的控件,它的每个元素都可以自定义显示方式。在本文中,我们会通过自定义 ItemsControl,实现一个横向滚动的列表。 首先,我们需要创建一个自定义控件,继承自 ItemsControl。在这个控件中,我们需要对 ItemContainerStyle 和 ItemsPanel 进行定义。 ItemContainerStyle 用于定义每个元素的样式,我们可以设置元素的大小、边距等属性。在本例中,我们设置元素宽度为 100,高度为自适应,并设置左右边距为 5。 ItemsPanel 用于定义元素的排列方式。在本例中,我们使用一个 StackPanel,设置 Orientation 为 Horizontal,这样子项就会水平排列。 下面是完整的代码实现: ```csharp using Xamarin.Forms; namespace MyNamespace { public class HorizontalItemsControl : ItemsControl { public HorizontalItemsControl() { // 设置样式 ItemContainerStyle = new Style(typeof(ViewCell)) { Setters = { new Setter { Property = ViewCell.WidthRequestProperty, Value = 100 }, new Setter { Property = ViewCell.MarginProperty, Value = new Thickness(5, 0) } } }; // 设置子项排列方式 ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal); } } } ``` 使用起来也非常简单,只需要在 XAML 中声明这个控件即可: ```xml <local:HorizontalItemsControl ItemsSource="{Binding MyItems}"> <local:HorizontalItemsControl.ItemTemplate> <DataTemplate> <!-- 这里放置每个元素的显示内容 --> </DataTemplate> </local:HorizontalItemsControl.ItemTemplate> </local:HorizontalItemsControl> ``` 其中,MyItems 指定了要显示的数据源,ItemTemplate 指定了每个元素的显示方式。 这样,我们就实现了一个简单的横向滚动列表。当然,这只是一个简单的示例,实际使用中可能还需要进行更多的定制和优化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值