求一个做美食视频的网站,陕西手机网站建设,wordpress编辑器段间距,如何制作网站教程视频第7章#xff1a;数据绑定
本章目标 理解路由事件 掌握键盘输入事件 掌握鼠标输入事件 掌握多点触控输入事件
数据绑定概述
什么是数据绑定
将WPF中的至少一个带有依赖项属性的两个对象的两个属性进行绑定#xff0c;使某一个依赖项属性可以更新和它绑定的属性的功…第7章数据绑定
本章目标 理解路由事件 掌握键盘输入事件 掌握鼠标输入事件 掌握多点触控输入事件
数据绑定概述
什么是数据绑定
将WPF中的至少一个带有依赖项属性的两个对象的两个属性进行绑定使某一个依赖项属性可以更新和它绑定的属性的功能。
数据绑定涉及两个方面一个是绑定源再一个是绑定目标。绑定源即空间绑定所使用的源数据绑定目标即数据显示的控件。
对于绑定源在WPF中可以是以下4种
CLR对象可以绑定到CLR类的公开属性/子属性/索引器上ADO.net 对象例如DataTable/DataView 等。XML文件使用XPath 进行解析DependencyObject: 绑定到依赖项属性上即控件绑定控件。
对于绑定目标必须是WPF中的DependencyObject,将数据绑定到其依赖项属性上。 数据绑定的绑定源
使用接口 INoitfyPropertyChanged使用依赖属性 DependecyProperty
数据绑定的语法
{Binding ElementName元素名,Path属性,Mode绑定模式}绑定DependencyObject对象
根据元素名称绑定 Window x:ClassWPF_CH07_Demo01.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WPF_CH07_Demo01mc:IgnorabledTitleMainWindow Height450 Width800StackPanelTextBox x:Nametxt1 TextWrappingWrap Height100/TextBoxTextBlock Text{Binding ElementNametxt1,PathText}/TextBlock/StackPanel/Window
相对于自身或父元素绑定 Window x:ClassWPF_CH07_Demo01.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WPF_CH07_Demo01mc:IgnorabledTitleMainWindow Height450 Width800StackPanel Height150Button Height40 Content{Binding RelativeSource{RelativeSource ModeSelf},PathHeight} /Button Height40 Content{Binding RelativeSource{RelativeSource ModeFindAncestor, AncestorTypeStackPanel},PathHeight}//StackPanel/Window
绑定CLR对象
绑定到单个对象 xaml:
Window x:ClassWPF_CH07_Demo01.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WPF_CH07_Demo01mc:IgnorabledNamemainWindowTitleMainWindow Height450 Width800StackPanelLabel HorizontalAlignmentCenter FontSize20学生信息/LabelGridGrid.RowDefinitionsRowDefinition/RowDefinition/RowDefinition//Grid.RowDefinitionsGrid.ColumnDefinitionsColumnDefinition Width200/ColumnDefinition//Grid.ColumnDefinitionsLabel Grid.Row0 Grid.Column0 Content名字 HorizontalAlignmentRight/TextBox Grid.Row0 Grid.Column1 Text{Binding Name} /Label Grid.Row1 Grid.Column0 Content性别 HorizontalAlignmentRight/TextBox Grid.Row1 Grid.Column1 Text{Binding Sex} /Label Grid.Row2 Grid.Column0 Content年龄 HorizontalAlignmentRight/TextBox Grid.Row2 Grid.Column1 Text{Binding Age} //Grid/StackPanel
/Window
cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WPF_CH07_Demo01
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();//初始化数据Student student new Student() { Name 孙悟空, Sex 男, Age 18 };//指定数据上下文this.DataContext student;}}/// summary/// 学生类/// /summarypublic class Student{public string Name { get; set; }public string Sex { get; set; }public int Age { get; set; }}
}
绑定到集合元素-1 xaml代码
Window x:ClassWPF_CH07_Demo01.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WPF_CH07_Demo01mc:IgnorabledTitleMainWindow Height450 Width800StackPanelTextBlock姓名/TextBlockListBox ItemsSource{Binding NameList}/ListBox/StackPanel/Window
cs代码
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WPF_CH07_Demo01
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();//初始化数据this.NameList new ObservableCollectionstring();this.NameList.Add(孙悟空);this.NameList.Add(猪八戒);this.NameList.Add(沙悟净);//指定数据上下文this.DataContext this;}/// summary/// 姓名集合/// /summarypublic ObservableCollectionstring NameList { get; set; }}
}
绑定到集合元素-2 xaml:
Window x:ClassWPF_CH07_Demo01.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WPF_CH07_Demo01mc:IgnorabledTitleMainWindow Height450 Width800StackPanelTextBlock学生/TextBlockListBox ItemsSource{Binding StuList}ListBox.ItemTemplateDataTemplateUniformGrid Columns3TextBlock Text{Binding Name} Margin5/TextBlock Text{Binding Sex} Margin5/TextBlock Text{Binding Age} Margin5//UniformGrid/DataTemplate/ListBox.ItemTemplate/ListBox/StackPanel/Window
cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WPF_CH07_Demo01
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();//初始化数据this.StuList new ObservableCollectionStudent();this.StuList.Add(new Student { Name孙悟空,Sex男,Age150});this.StuList.Add(new Student { Name 猪八戒, Sex 男, Age 120 });this.StuList.Add(new Student { Name 沙悟净, Sex 男, Age 100 });//指定数据上下文this.DataContext this;}/// summary/// 姓名集合/// /summarypublic ObservableCollectionStudent StuList { get; set; }}/// summary/// 学生类/// /summarypublic class Student{/// summary/// 姓名/// /summarypublic string Name { get; set; }/// summary/// 性别/// /summarypublic string Sex { get; set; }/// summary/// 年龄/// /summarypublic int Age { get; set; }}
}
MVVM模式实现数据绑定 xaml:
Window x:ClassWPF_CH07_Demo01.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WPF_CH07_Demo01mc:IgnorabledTitleMainWindow Height450 Width800StackPanelTextBlock Text{Binding School} /TextBlock Text{Binding TeacherCount} /TextBlock Text{Binding StudentCount} //StackPanel
/Window
cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WPF_CH07_Demo01
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();//初始化数据MainViewModel mvm new MainViewModel();mvm.School 广创;mvm.TeacherCount 20;mvm.StudentCount 500;//指定数据上下文this.DataContext mvm;}}/// summary/// 页面视图 数据 模型/// /summarypublic class MainViewModel{/// summary/// 学校/// /summarypublic string School { get; set; }/// summary/// 教师人数/// /summarypublic int TeacherCount { get; set; }/// summary/// 学生人数/// /summarypublic int StudentCount { get; set; }}
}
属性改变通知进行数据绑定 xaml:
Window x:ClassWPF_CH07_Demo01.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WPF_CH07_Demo01mc:IgnorabledNamemainWindowTitleMainWindow Height450 Width800StackPanelLabel HorizontalAlignmentCenter FontSize20学生信息/LabelGridGrid.RowDefinitionsRowDefinition/RowDefinition/RowDefinition/RowDefinition/RowDefinition//Grid.RowDefinitionsGrid.ColumnDefinitionsColumnDefinition Width200/ColumnDefinition//Grid.ColumnDefinitionsLabel Grid.Row0 Grid.Column0 Content名字 HorizontalAlignmentRight/TextBox Grid.Row0 Grid.Column1 Text{Binding Name} /Label Grid.Row1 Grid.Column0 Content性别 HorizontalAlignmentRight/TextBox Grid.Row1 Grid.Column1 Text{Binding Sex} /Label Grid.Row2 Grid.Column0 Content年龄 HorizontalAlignmentRight/TextBox Grid.Row2 Grid.Column1 Text{Binding Age} /Button Namebtn1 Grid.Row3 Grid.Column1 Width120 Height40 Margin5 Clickbtn1_Click显示对象信息/ButtonButton Namebtn2 Grid.Row4 Grid.Column1 Width120 Height40 Margin5 Clickbtn2_Click清除对象信息/Button/Grid/StackPanel
/Window
cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WPF_CH07_Demo01
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{Student student;public MainWindow(){InitializeComponent();//初始化数据student new Student() { Name 孙悟空, Sex 男, Age 18 };//指定数据上下文this.DataContext student;}/// summary/// 显示对象信息/// /summary/// param namesender/param/// param namee/paramprivate void btn1_Click(object sender, RoutedEventArgs e){MessageBox.Show(this.student.ToString());}/// summary/// 清空对象内容/// /summary/// param namesender/param/// param namee/paramprivate void btn2_Click(object sender, RoutedEventArgs e){this.student.Name null;this.student.Sex null;this.student.Age null;}}/// summary/// 页面视图 数据 模型/// /summarypublic class Student : INotifyPropertyChanged{private string name;public string Name{get { return name; }set{name value;OnPropertyChanged(Name);//引发事件}}private string sex;public string Sex{get { return sex; }set{sex value;OnPropertyChanged(Sex);//引发事件}}private int? age;public int? Age{get { return age; }set{age value;OnPropertyChanged(Age);//引发事件}}/// summary/// 属性改变事件/// /summarypublic event PropertyChangedEventHandler PropertyChanged;/// summary/// 事件处理函数/// /summary/// param nameproperty/parampublic void OnPropertyChanged(string property){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));}public override string ToString(){return string.Format(姓名{0},性别{1}年龄{2}, Name, Sex, Age);}}
}
绑定ADO.NET 对象 xaml:
Window x:ClassWPF_CH07_Demo01.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WPF_CH07_Demo01mc:IgnorabledNamemainWindowTitleMainWindow Height450 Width800StackPanelTextBlock学生/TextBlockListBox ItemsSource{Binding StuListData}ListBox.ItemTemplateDataTemplateUniformGrid Columns3TextBlock Text{Binding Name} Margin5/TextBlock Text{Binding Sex} Margin5/TextBlock Text{Binding Age} Margin5//UniformGrid/DataTemplate/ListBox.ItemTemplate/ListBox/StackPanel
/Window
cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WPF_CH07_Demo01
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();//初始化数据this.InitialData();//指定数据上下文this.DataContext this;}/// summary/// 初始化数据/// /summarypublic void InitialData(){this.StuListData new DataTable();//列this.StuListData.Columns.Add(Name, typeof(string));this.StuListData.Columns.Add(Sex, typeof(string));this.StuListData.Columns.Add(Age, typeof(int));//行DataRow row1 this.StuListData.NewRow();row1[Name] 孙悟空;row1[Sex] 男;row1[Age] 18;DataRow row2 this.StuListData.NewRow();row2[Name] 猪八戒;row2[Sex] 男;row2[Age] 19;DataRow row3 this.StuListData.NewRow();row3[Name] 沙悟净;row3[Sex] 男;row3[Age] 20;//添加到数据表this.StuListData.Rows.Add(row1);this.StuListData.Rows.Add(row2);this.StuListData.Rows.Add(row3);}/// summary/// 学生数据表/// /summarypublic DataTable StuListData { get; set; }}}
绑定XML对象 xaml:
Window x:ClassWPF_CH07_Demo01.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WPF_CH07_Demo01mc:IgnorabledNamemainWindowTitleMainWindow Height450 Width800StackPanelListBox ItemsSource{Binding Source{StaticResource PeopleData}}ListBox.ItemTemplateDataTemplateUniformGrid Columns2TextBlock Width200 Text{Binding XPathName}/TextBlock Width200 Text{Binding XPathAge}//UniformGrid/DataTemplate/ListBox.ItemTemplate/ListBox/StackPanel
/Window
xml:
?xml version1.0 encodingutf-8 ?
PeoplePerson Name孙悟空 Age30 /Person Name猪八戒 Age25 /Person Name沙悟净 Age35 /
/PeopleApp.xaml:
Application x:ClassWPF_CH07_Demo01.Appxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:WPF_CH07_Demo01StartupUriMainWindow.xamlApplication.ResourcesXmlDataProvider x:KeyPeopleData XPathPeople/Person Sourcedata.xml//Application.Resources
/Application
WPF的5种绑定模式
OneWay源变就更新目标属性TwoWay源变就更新目标并且目标变就更新源OneTime只根据源来设置目标以后都不会变OneWayToSource与OneWay相反Default可以单向或双向是靠被值定的源或目标是否有get或set来指定的 xaml:
Window x:ClassWPF_CH07_Demo01.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WPF_CH07_Demo01mc:IgnorabledNameMainWindow1TitleMainWindow Height450 Width800StackPanelSlider Namesb1 Minimum0 Maximum100 Value50 Width300 Height 30 SmallChange1/GridGrid.RowDefinitionsRowDefinition/RowDefinition/RowDefinition/RowDefinition//Grid.RowDefinitionsGrid.ColumnDefinitionsColumnDefinition Width300/ColumnDefinition//Grid.ColumnDefinitionsLabel Grid.Row0 Grid.Column0 ContentOneWay/TextBox Grid.Row0 Grid.Column1 Text{Binding ElementNamesb1,PathValue,ModeOneWay}/Label Grid.Row1 Grid.Column0 ContentOneWayToSource/TextBox Grid.Row1 Grid.Column1 Text{Binding ElementNamesb1,PathValue,ModeOneWayToSource}/Label Grid.Row2 Grid.Column0 ContentTwoWay/TextBox Grid.Row2 Grid.Column1 Text{Binding ElementNamesb1,PathValue,ModeTwoWay}/Label Grid.Row3 Grid.Column0 ContentOneTime/TextBox Grid.Row3 Grid.Column1 Text{Binding ElementNamesb1,PathValue,ModeOneTime}//Grid/StackPanel
/Window
课后作业
略