WPF Demo8

本文介绍了一个使用WPF进行界面设计的例子,并通过C#实现了数据绑定功能,展示了如何将一个学生列表显示在界面上,并实时更新选定的学生姓名。
namespace Demo10
{
    public class Student
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
}

  

<Window x:Class="Demo10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBlock Margin="5" Text="Student Name:"/>
        <TextBox Margin="5" x:Name="txtName"/>
        
        <TextBlock Margin="5" Text="Student List:"/>
        <ListBox Margin="5" x:Name="lsbList"/>
    </StackPanel>
</Window>
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;

namespace Demo10
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<Student> stuList = new List<Student>()
            {
                new Student(){Name ="Tim"},
                new Student(){Name ="Tom"},
                new Student(){Name ="JIM"},
                new Student(){Name ="Kite"},
                new Student(){Name ="quanquan"},
            };

            this.lsbList.ItemsSource = stuList;
            this.lsbList.DisplayMemberPath = "Name";

            Binding binding = new Binding("SelectedItem.Name") { Source=this.lsbList};
            this.txtName.SetBinding(TextBox.TextProperty,binding);
        }
    }
}

转载于:https://www.cnblogs.com/YYkun/p/6867244.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值