【原创】WPF DataGrid ComboBox扩展用法(绑定List<T>和Hashtable)
| 分类: C#.NET开发 |
之前看到的关于DataGrid中
DataGridComboBoxColumn都是关于绑定Enum的,很少有绑定List与Hashtable的,这里呐我就对我最近的项目上需要用到的,自己总结的一个方法做一下介绍。
public partial class MainWindow :
Window
{
public static List Projects = new List();
public static List Users = new List();
public static Hashtable Dutys = new
Hashtable();
public static List WorkLoads = new List();
public static Hashtable Percents = new
Hashtable();
public MainWindow()
{
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);
InitializeComponent();
}
}
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;
}
}
public
static List Projects = new List();
public
static List Users = new List();
public
static Hashtable Dutys = new Hashtable();
public
static List WorkLoads = new List();
public
static Hashtable Percents = new Hashtable();
xmlns:local="clr-namespace:WpfApplication3"
< DataGrid
ItemsSource="{x:Static local:MainWindow.Projects}"
>
<
DataGrid.Columns>
<
DataGridTextColumn Header="编号" Binding="{Binding
ID}"/>
<
DataGridComboBoxColumn Header="姓名"
ItemsSource="{x:Static local:MainWindow.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:MainWindow.Dutys}"
DisplayMemberPath="Value"
SelectedValuePath="Value" TextBinding="{Binding
Path=Duty}">
<
DataGridComboBoxColumn Header="参与小时数(h)"
ItemsSource="{x:Static local:MainWindow.WorkLoads}"
TextBinding="{Binding
Path=WorkLoad}">
<
DataGridComboBoxColumn Header="参与工作量(%)"
ItemsSource="{x:Static local:MainWindow.Percents}"
DisplayMemberPath="Value"
SelectedValuePath="Value" TextBinding="{Binding
Path=Percent}"> <
/DataGridComboBoxColumn>
<
/DataGrid.Columns>
<
/DataGrid>
通过ItemsSource属性绑定DataGridComboBoxColumn有三种方式,具体参考
http://s15/mw690/82544279zx6DB1COOvhcb&690DataGrid ComboBox扩展用法(绑定List<T>和Hashtable)" TITLE="【原创】WPF DataGrid ComboBox扩展用法(绑定List<T>和Hashtable)" />
这里我使用x:Static代码实体来实现数据绑定。
首先我在MainWindow.xaml.cs里面定义我自己的数据。
我所定义的静态变量是这些,其中Projects为DataGrid需要绑定的数据,其余都是DataGrid中DataGridComboBoxColumn列需要绑定的数据。
OK,我们来看一看,那么前台XAML代码是怎样实现绑定的呐!
需要声明一下命名空间:
绑定数据
这里可以看出,每个DataGridComboBoxColumn列,无论绑定什么数据,都会调用ItemsSource绑定下拉数据源,TextBinding绑定Projects对应属性的值。
其次,除了List不用绑定DisplayMemberPath与SelectedValuePath外,其他都需要绑定,List是绑定的User的Name,Hashtable绑定的是Value。
希望以上总结对大家能有帮助,如有疑问请联系我。
QQ 284362096

加载中…