标签:
WPF是分离UI和Logic的最佳工具,不同于Window Form的事件驱动原理,WPF采用的是数据驱动,让UI成为了Logic的附属,达到分离的效果。
下面以一个简单的data binding例子为例来阐述wpf
在一个界面里有一个textbox和button,按button后textbox会不停地在Student类的实例的Name属性上增加“Name”,而TextBox显示该实例的Name属性
Student类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.ComponentModel; 7 8 namespace WpfApplication1 9 { 10 class Student : INotifyPropertyChanged 11 { 12 public event PropertyChangedEventHandler PropertyChanged; 13 14 private string name; 15 16 public string Name 17 { 18 get { return name; } 19 set { 20 name = value; 21 if (this.PropertyChanged != null) 22 { 23 this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name")); 24 } 25 } 26 } 27 } 28 }
MainWindow.xmal:
1 <Window x:Class="WpfApplication1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="Simple Binding" Height="110" Width="300"> 5 <StackPanel> 6 <TextBox x:Name="textBoxName" BorderBrush="Black" Margin="5"/> 7 <Button Content="Add Age" Margin="5" Click="Button_Click"/> 8 </StackPanel> 9 </Window>
MainWindow.xmal.cs:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 16 namespace WpfApplication1 17 { 18 /// <summary> 19 /// Interaction logic for MainWindow.xaml 20 /// </summary> 21 public partial class MainWindow : Window 22 { 23 Student stu; 24 public MainWindow() 25 { 26 InitializeComponent(); 27 28 stu = new Student(); 29 Binding binding = new Binding(); 30 binding.Source = stu; 31 binding.Path = new PropertyPath("Name"); 32 33 BindingOperations.SetBinding(this.textBoxName, TextBox.TextProperty, binding); 34 } 35 36 private void Button_Click(object sender, RoutedEventArgs e) 37 { 38 stu.Name += "Name"; 39 } 40 } 41 }
可以看到在对Student类的Name属性加上PropertyChanged.Invoke函数后就能实现该属性的被绑定了。
MainWindow.xmal只是一个UI,并没有什么Logic部分
而在MainWindow.xmal.cs中,将textBoxName的TextProperty和stu的Name属性进行了绑定,Button_Click事件则只是单单地为Logic了,没有UI的代码,彻彻底底地将UI上数据变化的工作交给了binding自动处理。
从这里可以看出,WPF真的是一个非常牛叉的技术,将UI和Logic完美分离,让UI设计者和业务逻辑者可以并行地开发项目,减少了项目开发时间
当然上面这段代码还可以进行优化
MainWindow.xmal.cs:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 16 namespace WpfApplication1 17 { 18 /// <summary> 19 /// Interaction logic for MainWindow.xaml 20 /// </summary> 21 public partial class MainWindow : Window 22 { 23 Student stu; 24 public MainWindow() 25 { 26 InitializeComponent(); 27 28 this.textBoxName.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = stu = new Student() }); 29 } 30 31 private void Button_Click(object sender, RoutedEventArgs e) 32 { 33 stu.Name += "Name"; 34 } 35 } 36 }
标签:
原文地址:http://www.cnblogs.com/yingzhongwen/p/4718432.html