zoukankan      html  css  js  c++  java
  • WPF(初识DataTemplate)

    <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"
                     >
                <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>
    </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; }
        }
    }
    


  • 相关阅读:
    HDU 5835 Danganronpa(弹丸论破)
    HDU 5813 Elegant Construction(优雅建造)
    HDU 5831 Rikka with Parenthesis II(六花与括号II)
    HDU 5810 Balls and Boxes(盒子与球)
    HDU 5818 Joint Stacks(联合栈)
    Dream Team(最小生成树)
    带权并查集
    HDU 1495 非常可乐(BFS)
    I Hate It(线段树+更新)
    Just a Hook(线段树+区间更新)
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671674.html
Copyright © 2011-2022 走看看