1. 程式人生 > 其它 >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;
}
}