WPF DataGrid+ComboBox控件的应用

1、实现功能
本文以员工工作统计为例介绍DataGrid+ComboBox控件的应用,左侧为部门、右侧为部门成员信息,通过部门切换,右侧的内容发生变化。
2、前端代码

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.ColumnSpan="2" Text="员工工作统计例程(DataGrid+ComboBox)" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="12"/>
        <ListView x:Name="listViewMenu" Grid.Column="0" Grid.Row="1" FontSize="16" SelectionChanged="listViewMenu_SelectionChanged" Margin="10 0 0 10"/>
        <DataGrid Grid.Row="1" Grid.Column="1" x:Name="dataGridList" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="编号" Binding="{Binding ID}"/>
                <DataGridTextColumn Header="姓名" Binding="{Binding Path=Name}"/>
                <DataGridTextColumn Header="起始日期" Binding="{Binding StartDateTime}" />
                <DataGridTextColumn Header="截止日期" Binding="{Binding EndDateTime}" />
                <DataGridComboBoxColumn Header="职位" ItemsSource="{x:Static local:MainWindow.Dutys}" DisplayMemberPath="Value" SelectedValuePath="Value" TextBinding="{Binding Path=Duty}"></DataGridComboBoxColumn>
                <DataGridComboBoxColumn Header="工作时间(h)" ItemsSource="{x:Static local:MainWindow.WorkLoads}" TextBinding="{Binding Path=WorkLoad}"></DataGridComboBoxColumn>
                <DataGridComboBoxColumn Header="工作量(%)" ItemsSource="{x:Static local:MainWindow.Percents}" DisplayMemberPath="Value" SelectedValuePath="Value" TextBinding="{Binding Path=Percent}"></DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button Grid.Row="2" Grid.ColumnSpan="2" Content="确定" FontSize="18" Width="100" Height="50"/>
    </Grid>

3、后台代码

namespace WpfPageApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static List<string> Department=new List<string>();
        public static List<Project> Projects = new List<Project>();
        public static List<User> Users = new List<User>();
        public static Hashtable Dutys = new Hashtable();
        public static List<double> WorkLoads = new List<double>();
        public static Hashtable Percents = new Hashtable();
        public MainWindow()
        {
            InitializeComponent();
            DepartmentSet();
            HumanResourceSet();
            CommonSet();
        }
        private void DepartmentSet()
        {
            Department.Clear();
            Department.Add("人力资源部");
            Department.Add("运营部");
            listViewMenu.ItemsSource = Department;
        }
        private void HumanResourceSet()
        {
            Users.Clear();
            Users.Add(new User("张三"));
            Users.Add(new User("李四"));
            Users.Add(new User("王麻子"));
            Projects.Clear();
            Projects.Add(new Project("张三", DateTime.Now, DateTime.Now, "负责人"));
            Projects.Add(new Project("李四", DateTime.Now, DateTime.Now, "参与者"));
            Projects.Add(new Project("王麻子", DateTime.Now, DateTime.Now, "参与者"));
            dataGridList.ItemsSource = null;
            dataGridList.ItemsSource = Projects;
            Dutys.Clear();
            Dutys.Add(0, "负责人");
            Dutys.Add(1, "参与者");
            Dutys.Add(2, "观察员");
            Dutys.Add(3, "酱油");
        }

        private void OperationSet()
        {
            Users.Clear();
            Users.Add(new User("Bill"));
            Users.Add(new User("Tom"));
            Users.Add(new User("John"));
            Users.Add(new User("Alan"));
            Users.Add(new User("Simon"));
            Projects.Clear();
            Projects.Add(new Project("Bill", DateTime.Now, DateTime.Now, "leader"));
            Projects.Add(new Project("Tom", DateTime.Now, DateTime.Now, "participant"));
            Projects.Add(new Project("John", DateTime.Now, DateTime.Now, "participant"));
            Projects.Add(new Project("Alan", DateTime.Now, DateTime.Now, "participant"));
            Projects.Add(new Project("Simon", DateTime.Now, DateTime.Now, "participant"));
            dataGridList.ItemsSource = null;
            dataGridList.ItemsSource = Projects;
            Dutys.Clear();
            Dutys.Add(0, "leader");
            Dutys.Add(1, "participant");
            Dutys.Add(2, "observer");
            Dutys.Add(3, "soy sauce");
        }
        private void CommonSet()
        {
            WorkLoads.Add(10);
            WorkLoads.Add(20);
            WorkLoads.Add(40);
            WorkLoads.Add(80);

            Percents.Add(1, 20);
            Percents.Add(2, 40);
            Percents.Add(3, 60);
            Percents.Add(4, 80);
        }

        private void listViewMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listViewMenu.SelectedIndex == 0)
            {
                HumanResourceSet();
            }
            else
            {
                OperationSet();
            }
        }
    }
    public class User
    {
        static int num = 0;
        public int Id { get; set; }
        public string Name { get; set; }
        public User(string name)
        {
            Id = num++;
            Name = name;
        }
    }
    public class Project
    {
        static int num = 0;
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime StartDateTime { get; set; }
        public DateTime EndDateTime { get; set; }
        public string Duty { get; set; }
        public double WorkLoad { get; set; }
        public int Percent { get; set; }
        public Project(string name, DateTime startDateTime, DateTime endDateTime, string duty)
        {
            ID = num++;
            Name = name;
            StartDateTime = startDateTime;
            EndDateTime = endDateTime;
            Duty = duty;
            WorkLoad = 40;
            Percent = 20;
        }
    }
}

4、运行结果
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值