zoukankan      html  css  js  c++  java
  • WPF(Binding集合对象数据源)

    <Window x:Class="TestOfBindingItemsControl.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 x:Name="stackPanel" Background="LightBlue" >
            <TextBlock Text="StudentID:" Margin="5" />
            <TextBox x:Name="textBoxId" Margin="5" />
            <TextBlock Text="StudentList:" FontWeight="Bold"
                       Margin="5" />
            <ListBox x:Name="listBoxStudents" 
                     Height="110" 
                     Margin="5"
                     />
            
        </StackPanel>
    </Window>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace TestOfBindingItemsControl
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            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 = "Vine",Age = 25},
                      new Student(){Id = 5,Name = "Mike",Age = 24}     
                };
    
                // 为ListBox设置Binding
                this.listBoxStudents.ItemsSource = stuList;
                this.listBoxStudents.DisplayMemberPath = "Name";
    
                Binding binding = new Binding("SelectedItem.Id")
                                      {
                                          Source = this.listBoxStudents
                                      };
    
                this.textBoxId.SetBinding(TextBox.TextProperty, binding);
            }
        }
    
        public class Student
        {
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public int Age { get; set; }
        }
    }
    


  • 相关阅读:
    Java Swing打猎射击游戏源码
    php实现在线下载程序安装包功能
    WP
    双人对战的球类游戏IOS源码
    非常不错的新闻客户端应用安卓源码
    创业建议:如何写挖人邮件
    wp8路线跟踪应用源码详细说明
    wp版笔记本应用源码
    旅游风景展示应用源码iPad版
    bitset优化背包
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671675.html
Copyright © 2011-2022 走看看