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         }

 

 

       

转载于:https://www.cnblogs.com/sxhNicole/archive/2012/10/17/WPF_DataGrid_Binding.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值