Silverlight的ListBox和DataGrid绑定
Demo1
前台:<my:DataGrid x:Name="GridPerson" AutoGenerateColumns="False" AlternatingRowBackground="Blue" GridlinesVisibility="All" Width="400" Height="300" Canvas.Top="10" Canvas.Left="10">
<my:DataGrid.Columns>
<my:DataGridTextBoxColumn Header="Id" DisplayMemberBinding="{Binding Id}" ></my:DataGridTextBoxColumn>
<my:DataGridTextBoxColumn Header="Name" DisplayMemberBinding="{Binding Name}" ></my:DataGridTextBoxColumn>
<my:DataGridTextBoxColumn Header="Email" DisplayMemberBinding="{Binding Email}" ></my:DataGridTextBoxColumn>
<my:DataGridTemplateColumn Header="Del">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Email}"></TextBox>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>
后台:
void BindData()
{
System.Collections.Generic.List<PersonInfo> personlist = new List<PersonInfo>();
personlist.Add(new PersonInfo() { Id="0008", Name="陈国成8", Email="guocheng.chen8@rising.com" });
personlist.Add(new PersonInfo() { Id = "0001", Name = "陈国成1", Email = "guocheng.chen1@rising.com",Deleted=true });
personlist.Add(new PersonInfo() { Id = "0002", Name = "陈国成2", Email = "guocheng.chen2@rising.com", Deleted = true });
personlist.Add(new PersonInfo() { Id = "0003", Name = "陈国成3", Email = "guocheng.chen3@rising.com", Deleted = false });
this.GridPerson.ItemsSource = personlist;
}
Demo2
<ListBox x:Name="LstPerson">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
this.LstPerson.ItemsSource = personlist;