原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二)
上次学了点点Binding的皮毛, 然后就做别的事去了, 等回头再来看WPF的时候, 哈忘记了~
于是写个例子补一下, 在继续学习Binding.
1, 首先准备好一个类
public class Hero { public Hero(int id, string name, string skill, bool hasM) { this.Name = name; this.Id = id; this.Skill = skill; this.HasM = hasM; } public int Id { get; set; } public string Name { get; set; } public string Skill { get; set; } public bool HasM { get; set; } }
2, 在MainWindow中准备好数据
Dictionary<string, Hero> map = new Dictionary<string, Hero>(); private void InitDictionary() { Hero hero1 = new Hero(1, "刘备", "哭泣", true); map.Add(hero1.Name, hero1); Hero hero2 = new Hero(2, "官羽", "贪污", false); map.Add(hero2.Name, hero2); Hero hero3 = new Hero(3, "黄忠", "射击", true); map.Add(hero3.Name, hero3); Hero hero4 = new Hero(4, "魏延", "突击", true); map.Add(hero4.Name, hero4); Hero hero5 = new Hero(5, "马超", "单挑", false); map.Add(hero5.Name, hero5); Hero hero6 = new Hero(6, "曹仁", "防守", true); map.Add(hero6.Name, hero6); }
然后XAML这边是这样的
1, 先准备好template
<Window.Resources> <DataTemplate x:Key="nameDT"> <TextBlock x:Name="textBoxName" Text="{Binding Name}" /> </DataTemplate> <DataTemplate x:Key="skillDT"> <TextBlock x:Name="textBoxSkill" Text="{Binding Skill}" /> </DataTemplate> <DataTemplate x:Key="hmDT"> <CheckBox x:Name="checkBoxJob" IsChecked="{Binding HasM}" /> </DataTemplate> </Window.Resources>
2, 界面
<Grid Margin="5" > <ListView x:Name="listViewStudent"> <ListView.View> <GridView> <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" /> <GridViewColumn Header="姓名" CellTemplate="{StaticResource nameDT}" /> <GridViewColumn Header="技术" CellTemplate="{StaticResource skillDT}" /> <GridViewColumn Header="已婚" CellTemplate="{StaticResource hmDT}" /> </GridView> </ListView.View> </ListView> </Grid>
3, 就是上一篇笔记中记载的Binding方法了
public Window1() { InitializeComponent(); InitDictionary(); Binding binding = new Binding(); binding.Source = map; binding.Path = new PropertyPath("Values"); listViewStudent.SetBinding(ListView.ItemsSourceProperty, binding); }
好了, 运行!一切OK~
界面出来啦
等等! 好像和上次有点不太一样, 少了一步吧? 数据源没有实现INotifyPropertyChanged接口呢.
先不急, 测试下:
1, 加个button吧, XAML处的界面显示相关代码修改如下
<Grid Margin="5" > <Grid.RowDefinitions> <RowDefinition Height="3*" /> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <ListView x:Name="listViewStudent"> <ListView.View> <GridView> <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" /> <GridViewColumn Header="姓名" CellTemplate="{StaticResource nameDT}" /> <GridViewColumn Header="技术" CellTemplate="{StaticResource skillDT}" /> <GridViewColumn Header="已婚" CellTemplate="{StaticResource hjM}" /> </GridView> </ListView.View> </ListView> <Button Grid.Row="1" Content="给官老爷正名!" Click="Button_Click" /> </Grid>
2, 填函数
private void Button_Click(object sender, RoutedEventArgs e) { map["官羽"].Name = "关羽"; map["官羽"].Skill = "单挑"; }
F5运行, 点击按钮, 发现没有得到预期的改变, 表格显示的数据动也不动. 好吧, 看来的确没有Binding, 只是单次的赋值而已, 没有"数据驱动"呀!
那么来补上准备数据源这一步
修改后的Hero类代码如下, 为了便于比较, 实现了Skill, 而不实现Name等其他属性.
public class Hero : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged;//请注意 public Hero(int id, string name, string skill, bool hasJob) { this.Name = name; this.Id = id; this.Skill = skill; this.HasM = hasM; } public int Id { get; set; } public string Name { get; set; } public bool HasM { get; set; } private string skill; public string Skill { get { return skill; } set { skill = value; //触发事件//请注意 if (PropertyChanged != null) { PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Skill")); } } } }
好了, F5运行, 效果如下
嗯, 效果实现, Skill的确是变了, Name没变.
Binding学习笔记一的复习到此结束, 可以看新内容了~
啊! ! 刚才的Binding有点意思呢, 我不那么写Binding, 而是改成
public Window1() { InitializeComponent(); InitDictionary(); // Binding binding = new Binding(); // binding.Source = map; // binding.Path = new PropertyPath("Values"); // listViewStudent.SetBinding(ListView.ItemsSourceProperty, binding); listViewStudent.ItemsSource=map.Values; }
效果竟然一样! ! ! 这个可比textBox.Text的赋值智能多了呀...