• WPF dataGrid初步使用案例


    xaml 1 界面设计  

     

    1. <Window x:Class="Wpf.Template.One.Demo.NumberOne.dataGrid"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6. xmlns:local="clr-namespace:Wpf.Template.One.Demo.NumberOne"
    7. mc:Ignorable="d"
    8. Title="dataGrid" Height="450" Width="800" Loaded="Window_Loaded">
    9. <Grid>
    10. <DataGrid FontSize="20" Name="grid1" ItemsSource="{Binding}" AutoGenerateColumns="False">
    11. <DataGrid.Columns>
    12. <DataGridTextColumn Header="用户姓名" Binding="{Binding Name}">DataGridTextColumn>
    13. <DataGridTextColumn Header="年龄" Binding="{Binding Age}">DataGridTextColumn>
    14. <DataGridComboBoxColumn x:Name="depts" Header="部门" SelectedValueBinding="{Binding DeptId}">DataGridComboBoxColumn>
    15. DataGrid.Columns>
    16. DataGrid>
    17. Grid>
    18. Window>

     2 逻辑代码

      

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4. using System.Windows;
    5. using System.Windows.Controls;
    6. using System.Windows.Data;
    7. using System.Windows.Documents;
    8. using System.Windows.Input;
    9. using System.Windows.Media;
    10. using System.Windows.Media.Imaging;
    11. using System.Windows.Shapes;
    12. using Wpf.Template.One.Entity;
    13. namespace Wpf.Template.One.Demo.NumberOne
    14. {
    15. ///
    16. /// dataGrid.xaml 的交互逻辑
    17. ///
    18. public partial class dataGrid : Window
    19. {
    20. public dataGrid()
    21. {
    22. InitializeComponent();
    23. }
    24. private void Window_Loaded(object sender, RoutedEventArgs e)
    25. {
    26. // 集合创建
    27. List list = new List();
    28. list.Add( new userInfo() { Name="张三",Age=22,deptId=1001});
    29. list.Add(new userInfo() { Name = "李斯", Age = 32, deptId = 1002 });
    30. list.Add(new userInfo() { Name = "王武", Age = 42, deptId = 1003 });
    31. //集合 2
    32. List deplist = new List();
    33. deplist.Add(new depInfo() { Id = "1001", DeptName = "研发技术部" });
    34. deplist.Add(new depInfo() { Id = "1002", DeptName = "销售部" });
    35. deplist.Add(new depInfo() { Id = "1003", DeptName = "人事部" });
    36. this.depts.ItemsSource = deplist;
    37. //展示项 用部门名称
    38. this.depts.DisplayMemberPath = "DeptName";
    39. this.depts.SelectedValuePath = "Id";
    40. //直接赋值 list
    41. this.grid1.DataContext = list;
    42. }
    43. }
    44. }

     还有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; }
        }
    }
     

     

    效果 

     

    练习题

     降序排列怎么写?

    按照年龄的降序排序

     

  • 相关阅读:
    【虹科干货】逻辑数据库可能已经无法满足需求了!
    代码随想录——按奇偶排序数组II
    .NET周刊【3月第1期 2024-03-03】
    【notion enhancer安装】一个强大的笔记软件,可以实现侧边目录的notion
    Metersphere本地环境部署---非微服务版本
    iOS上架App Store的全攻略
    嵌入式系统中的GPIO控制与应用
    手把手教你用代码画架构图
    JAVA基础知识
    谁懂啊!自制的科普安全手册居然火了
  • 原文地址:https://blog.csdn.net/chenggong9527/article/details/126391425