WPF DataGrid应用篇
1关于枚举类型的应用
<!--xmlns:core="clr-namespace:System;assembly=mscorlib" 命名空间引用>
<!--资源字典引用>
<Window.Resources> <ResourceDictionary> <!--Create list of enumeration values--> <ObjectDataProvider x:Key="myEnum" MethodName="GetValues" ObjectType="{x:Type core:Enum}"> <ObjectDataProvider.MethodParameters> <x:Type Type="local:OrderStatus"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </ResourceDictionary> </Window.Resources>
<DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" > <DataGrid.Columns> <DataGridComboBoxColumn Header="Order Status" SelectedItemBinding="{Binding Status}" ItemsSource="{Binding Source={StaticResource myEnum}}" /> </DataGrid.Columns> </DataGrid>
public partial class CarLoadSetView : Window { public CarLoadSetView() { InitializeComponent(); ObservableCollection<Customer> custdata = GetData(); DG1.DataContext = custdata; } private ObservableCollection<Customer> GetData() { ObservableCollection<Customer> customers = new ObservableCollection<Customer>(); customers.Add(new Customer() { FirstName = "你好", LastName = "大家好", Status = OrderStatus.New,Email=new Uri ("http://www.contoso.com/") }); customers.Add(new Customer() { FirstName = "你好", LastName = "大家好", Status = OrderStatus.New }); customers.Add(new Customer() { FirstName = "你好", LastName = "大家好", Status = OrderStatus.New }); customers.Add(new Customer() { FirstName = "你好", LastName = "大家好", Status = OrderStatus.New }); customers.Add(new Customer() { FirstName = "你好", LastName = "大家好", Status = OrderStatus.New }); return customers; } } public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public Uri Email { get; set; } public bool IsMember { get; set; } public OrderStatus Status { get; set; } } public enum OrderStatus { None, New, Processing, Shipped, Received };
2、单元格的合并示例
<DataGrid AutoGenerateColumns="False" Name="dataGrid1" ItemsSource="{Binding}"> <DataGrid.Columns> <DataGridTemplateColumn Header="Name" > <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center" Margin="3,3,3,3"/> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn Header="Attributes" > <DataGridTemplateColumn.CellTemplate> <DataTemplate > <Grid> <Grid.RowDefinitions> <RowDefinition Height="50*"/> <RowDefinition /> <RowDefinition Height="50*"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Text="{Binding Path=Attributes.Attribute1}" VerticalAlignment="Center" Margin="3,3,3,3"/> <Line Grid.Row="1" Stroke="Black" Stretch="Fill" X2="1" VerticalAlignment="Center"/> <TextBlock Grid.Row="2" Text="{Binding Path=Attributes.Attribute2}" VerticalAlignment="Center" Margin="3,3,3,3"/> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>
public MainWindow() { InitializeComponent(); List<Test> list = GetData(); //populate list with your data here dataGrid1.DataContext = list; } private List<Test> GetData() { List<Test> list = new List<Test>(); list.Add(new Test("你好", "真漂亮", "不漂亮")); list.Add(new Test("你好", "真漂亮", "不漂亮")); return list; } public class Test { public Test(string name, string attribute1, string attribute2) { Name = name; Attributes = new Attribute(attribute1, attribute2); } public string Name { get; set; } public Attribute Attributes { get; set; } } public class Attribute { public Attribute(string attribute1, string attribute2) { Attribute1 = attribute1; Attribute2 = attribute2; } public string Attribute1 { get; set; } public string Attribute2 { get; set; } }