深入浅出WPF 第二部分(4)

本文介绍了如何在WPF应用程序中使用集合对象和ADO.NET对象作为数据源进行数据绑定。通过具体的XAML代码示例展示了列表控件的数据绑定过程,并演示了如何从数据库加载数据并显示在ListView组件中。

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

6.3.7 使用集合对象作为列表控件的ItemsSource

    <StackPanel x:Name="stackPanel" Background="LightBlue">
        <TextBlock Text="Student ID:" 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">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Id}" Width="30"/>
                        <TextBlock Text="{Binding Path=Name}" Width="60"/>
                        <TextBlock Text="{Binding Path=Age}" Width="30"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
     </StackPanel>

    public partial class MainWindow : Window
    {
        List<Student> stuList = new List<Student>() 
        {
            new Student(){Id = "0", Name = "Anders", Age = 27},
            new Student(){Id = "1", Name = "Xuyang", Age = 34}
        };

        public MainWindow()
        {
            InitializeComponent();

            this.listBoxStudents.ItemsSource = stuList;
            this.textBoxId.SetBinding(TextBox.TextProperty, new Binding("SelectedItem.Id") { Source = listBoxStudents, Mode = BindingMode.OneWay });
        }
    }

6.3.8 使用ADO.NET对象作为Binding的源

    <StackPanel x:Name="stackPanel" Background="LightBlue">
        <TextBlock Text="Student ID:" FontWeight="Bold" Margin="5"/>
        <TextBox x:Name="textBoxId" Margin="5"/>
        <TextBlock Text="Student List:" FontWeight="Bold" Margin="5"/>
        <ListView x:Name="listViewStudents" Height="130" Margin="5">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding Path=Id}"/>
                    <GridViewColumn Header="Name" Width="80" DisplayMemberBinding="{Binding Path=Name}"/>
                    <GridViewColumn Header="Age" Width="60" DisplayMemberBinding="{Binding Path=Age}"/>
                </GridView>
            </ListView.View>
        </ListView>
     </StackPanel>

        public MainWindow()
        {
            InitializeComponent();

            //Another bind source method
            //this.listViewStudents.ItemsSource=Load().DefaultView;
            
            this.listViewStudents.DataContext = Load();
            this.listViewStudents.SetBinding(ListView.ItemsSourceProperty, new Binding());

            this.textBoxId.SetBinding(TextBox.TextProperty, new Binding("SelectedItem.Id") { Source = listViewStudents, Mode = BindingMode.OneWay });
        }

        private DataTable Load()
        {
            var connStr = "Data Source=(local);Initial Catalog=AndersDB;Integrated Security=True";
            SqlCommand sqlComm = new SqlCommand("select * from Student", new SqlConnection(connStr));
            SqlDataAdapter sqlDA = new SqlDataAdapter(sqlComm);
            DataSet ds = new DataSet();
            sqlDA.Fill(ds);
            return ds.Tables[0];
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值