其他分享
首页 > 其他分享> > WPF DataGrid DataGridComboBoxColumn用法

WPF DataGrid DataGridComboBoxColumn用法

作者:互联网

xaml:

<Window x:Class="WPF入门.Window1"
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入门"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">

<DataGrid ItemsSource="{x:Static local:Window1.Projects}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="编号" Binding="{Binding ID}"/>
<DataGridComboBoxColumn Header="姓名" ItemsSource="{x:Static local:Window1.Users}" DisplayMemberPath="Name" SelectedValuePath="Name" TextBinding="{Binding Path=Name}"/>
<DataGridTextColumn Header="起始日期" Binding="{Binding StartDateTime}" />
<DataGridTextColumn Header="截止日期" Binding="{Binding EndDateTime}" />
<DataGridComboBoxColumn Header="职责" ItemsSource="{x:Static local:Window1.Dutys}" DisplayMemberPath="Value" SelectedValuePath="Value" TextBinding="{Binding Path=Duty}"></DataGridComboBoxColumn>
<DataGridComboBoxColumn Header="参与小时数(h)" ItemsSource="{x:Static local:Window1.WorkLoads}" TextBinding="{Binding Path=WorkLoad}"></DataGridComboBoxColumn>
<DataGridComboBoxColumn Header="参与工作量(%)" ItemsSource="{x:Static local:Window1.Percents}" DisplayMemberPath="Value" SelectedValuePath="Value" TextBinding="{Binding Path=Percent}"></DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Window>

C#

public partial class Window1 : Window
{
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 Window1()
{
InitializeComponent();

#region 测试数据
Users.Add(new User("陈凤伟"));
Users.Add(new User("郝鹏飞"));
Users.Add(new User("李栋"));
Users.Add(new User("陈力平"));

Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now, "负责人"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now, "参与者"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now, "参与者"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now, "参与者"));
Projects.Add(new Project("陈凤伟", DateTime.Now, DateTime.Now, "参与者"));

Dutys.Add(0, "负责人");
Dutys.Add(1, "参与者");
Dutys.Add(2, "观察员");
Dutys.Add(3, "酱油");

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);
#endregion
}

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

 

 

标签:get,DateTime,DataGrid,public,Add,new,WPF,Now,DataGridComboBoxColumn
来源: https://www.cnblogs.com/rowColumn/p/16073451.html