上次學了點點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, "官羽", "A錢", 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的指派智能多了呀...