DataGrid Binding
一、尝试了一下DataGrid绑定 list<>:
AutoGenerateColumns设置为True时,不需要自己列出Column,系统会自动按照List中相关字段,设置Column.
AutoGenerateColumns设置为False时,需要自己列出Column.
XAML中的代码:
1 <DataGrid AutoGenerateColumns="True" Height="140" HorizontalAlignment="Left" 2 Name="dataGrid1" VerticalAlignment="Top" Width="250" Grid.Column="1" Grid.Row="1" ItemsSource="{Binding Path=ListNote}"></DataGrid>
1 <DataGrid AutoGenerateColumns="False" Height="140" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="250" Grid.Column="1" Grid.Row="1" ItemsSource="{Binding Path=ListNote}"> 2 <DataGrid.Columns> 3 <DataGridTemplateColumn Header="Club" Width="45" IsReadOnly="True"> 4 <DataGridTemplateColumn.CellTemplate> 5 <DataTemplate> 6 <TextBlock Text="{Binding Path=Club}" HorizontalAlignment="Left"/> 7 </DataTemplate> 8 </DataGridTemplateColumn.CellTemplate> 9 </DataGridTemplateColumn> 10 11 <DataGridTemplateColumn Header="Level" Width="45" IsReadOnly="True"> 12 <DataGridTemplateColumn.CellTemplate> 13 <DataTemplate> 14 <TextBlock Text="{Binding Path=Level}" HorizontalAlignment="Center" /> 15 </DataTemplate> 16 </DataGridTemplateColumn.CellTemplate> 17 </DataGridTemplateColumn> 18 19 20 <DataGridTemplateColumn Header="Expiry Date" Width="80" IsReadOnly="True"> 21 <DataGridTemplateColumn.CellTemplate> 22 <DataTemplate> 23 <TextBlock> 24 <TextBlock.Text> 25 <Binding Path="ExpiryDate" StringFormat="MM/dd/yyyy"/> 26 </TextBlock.Text> 27 </TextBlock> 28 </DataTemplate> 29 </DataGridTemplateColumn.CellTemplate> 30 </DataGridTemplateColumn> 31 32 33 <DataGridTemplateColumn Header="Comments" Width="80" IsReadOnly="True"> 34 <DataGridTemplateColumn.CellTemplate> 35 <DataTemplate> 36 <TextBlock Text="{Binding Path=Comments}"/> 37 </DataTemplate> 38 </DataGridTemplateColumn.CellTemplate> 39 </DataGridTemplateColumn> 40 41 </DataGrid.Columns> 42 </DataGrid>
C#后台代码:
1 static List<Note> listNote = new List<Note>(); 2 3 public List<Note> ListNote 4 { 5 get { return listNote; } 6 set { listNote = value; } 7 } 8 public void Bind() 9 { 10 listNote.Add(new Note() { Club = "AAA", Level = 1, ExpiryDate = DateTime.Parse("10/18/2012"), Comments = "Good." }); 11 listNote.Add(new Note() { Club = "BBB", Level = 2, ExpiryDate = DateTime.Parse("09/22/2013"), Comments = "Just so so." }); 12 listNote.Add(new Note() { Club = "CCC", Level = 3, ExpiryDate = DateTime.Parse("12/07/2012"), Comments = "Bad." }); 13 dataGrid1.ItemsSource = listNote; 14 }
DataGrid的Binding需要用ItemSource指出需要绑定的List名。
1 private void btnCreate_Click(object sender, RoutedEventArgs e) 2 { 3 listNote.Add(new Note() { Club = cBoxClub.SelectedValue.ToString(), 4 Level = Convert.ToInt32(cBoxLevel.SelectedValue), 5 ExpiryDate=Convert.ToDateTime(txtExpieyDate.Text), 6 Comments=textBox2.Text}); 7 8 dataGrid1.Items.Refresh(); 9 }
但是,需要注意的是,List<>的Binding是一次性的;也就是说,当我们进行多次绑定时,应该选择ObservableCollection<>.
1 static ObservableCollection<Note> listNote = new ObservableCollection<Note>(); 2 3 public ObservableCollection<Note> ListNote 4 { 5 get { return listNote; } 6 set { listNote = value; } 7 }