xaml 1 界面设计
- <Window x:Class="Wpf.Template.One.Demo.NumberOne.dataGrid"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:Wpf.Template.One.Demo.NumberOne"
- mc:Ignorable="d"
- Title="dataGrid" Height="450" Width="800" Loaded="Window_Loaded">
- <Grid>
-
- <DataGrid FontSize="20" Name="grid1" ItemsSource="{Binding}" AutoGenerateColumns="False">
- <DataGrid.Columns>
- <DataGridTextColumn Header="用户姓名" Binding="{Binding Name}">DataGridTextColumn>
- <DataGridTextColumn Header="年龄" Binding="{Binding Age}">DataGridTextColumn>
-
- <DataGridComboBoxColumn x:Name="depts" Header="部门" SelectedValueBinding="{Binding DeptId}">DataGridComboBoxColumn>
- DataGrid.Columns>
-
-
-
-
-
- DataGrid>
- Grid>
- Window>
2 逻辑代码
- using System;
- using System.Collections.Generic;
- 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.Shapes;
-
- using Wpf.Template.One.Entity;
-
- namespace Wpf.Template.One.Demo.NumberOne
- {
- ///
- /// dataGrid.xaml 的交互逻辑
- ///
- public partial class dataGrid : Window
- {
- public dataGrid()
- {
- InitializeComponent();
- }
-
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- // 集合创建
- List
list = new List(); - list.Add( new userInfo() { Name="张三",Age=22,deptId=1001});
- list.Add(new userInfo() { Name = "李斯", Age = 32, deptId = 1002 });
- list.Add(new userInfo() { Name = "王武", Age = 42, deptId = 1003 });
-
-
- //集合 2
- List
deplist = new List(); - deplist.Add(new depInfo() { Id = "1001", DeptName = "研发技术部" });
- deplist.Add(new depInfo() { Id = "1002", DeptName = "销售部" });
- deplist.Add(new depInfo() { Id = "1003", DeptName = "人事部" });
-
- this.depts.ItemsSource = deplist;
- //展示项 用部门名称
- this.depts.DisplayMemberPath = "DeptName";
- this.depts.SelectedValuePath = "Id";
-
- //直接赋值 list
- this.grid1.DataContext = list;
-
-
-
- }
- }
- }
还有2个实体类
1 用户实体类
using System;
using System.Collections.Generic;
using System.Text;namespace Wpf.Template.One.Entity
{
class userInfo
{
public string Name { get; set; }
public int Age { get; set; }
public int deptId { get; set; }
}
}
2 部门实体类
using System;
using System.Collections.Generic;
using System.Text;namespace Wpf.Template.One.Entity
{
class depInfo
{
//prop 快捷输入
public string Id { get; set; }
///
/// 部门名称
///
public string DeptName { get; set; }
}
}
效果

练习题
降序排列怎么写?
按照年龄的降序排序