Winphone开发之数据绑定(3)
来源:互联网 发布:网络传销诈骗标语 编辑:程序博客网 时间:2024/06/05 20:47
这篇文章主要介绍怎么自定义Binding的源,通过CS代码指定Binding的源来初步构造整个数据绑定的流程。
下面是要绑定的类:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Text;using System.Threading.Tasks;namespace BindingLearning{ public class Person : INotifyPropertyChanged { private String name; public event PropertyChangedEventHandler PropertyChanged; public String Name { get { return name; } set { name = value; this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name")); } } }}
可以看到该类继承了INotifyPropertyChanged接口,然后声明了事件PropertyChanged,其中PropertyChangedEventHandler会当属性改变的时候抛出事件来通知UI,其中指明了属性名称为Name。
综上,该类会在Name属性被设置的时候发送数据更改的消息。
下面是XAML:
<phone:PhoneApplicationPage x:Class="BindingLearning.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True"> <Grid HorizontalAlignment="Left" Height="768" VerticalAlignment="Top" Width="480"> <Button Name="bt" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="178,470,0,0" Click="bt_Click"/> <TextBox Name="tb" HorizontalAlignment="Left" Height="72" Margin="14,222,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="456"/> </Grid></phone:PhoneApplicationPage>
只是简单声明了一个Button和一个TextBox
重要的是隐藏的CS文件:
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Navigation;using Microsoft.Phone.Controls;using Microsoft.Phone.Shell;using BindingLearning.Resources;using System.Windows.Data;namespace BindingLearning{ public partial class MainPage : PhoneApplicationPage { private Person p; // 构造函数 public MainPage() { InitializeComponent(); p = new Person(); Binding binding = new Binding(); binding.Source = p; binding.Path = new PropertyPath("Name"); this.tb.SetBinding(TextBox.TextProperty, binding); } private void bt_Click(object sender, RoutedEventArgs e) { p.Name += "hello"; } }}
构造了Person的实例p作为数据的源,然后制定里面的Name参数作为Path(Person里面有Name属性,并且我们刚刚也为它设置了监听),最后用SetBinding使得TextBox的TextProperty依赖属性和刚刚构造好的binding关联起来,最后的效果是,当p的Name属性被设置的时候,TextBox的Text依赖属性也会跟随改变。
然后我们监听了Button的点击事件,每点击一次就改变一下p的Name属性。
所以上面代码总体效果:
每当我点击一次Button,TextBox就会在原来的基础上不断地append字符串hello。
实例二:
和上面的实例差不多,但是用了DataContext,没有了Path
<phone:PhoneApplicationPage x:Class="PhoneApp6.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True"> <!--LayoutRoot 是包含所有页面内容的根网格--> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"/> <!--ContentPanel - 在此处放置其他内容--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBox Name="Tb" Text="{Binding Name}" HorizontalAlignment="Left" Height="72" Margin="0,241,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="456"/> </Grid> <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="170,422,0,0" Grid.Row="1" Click="Button_Click"/> </Grid></phone:PhoneApplicationPage>
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Navigation;using Microsoft.Phone.Controls;using Microsoft.Phone.Shell;using PhoneApp6.Resources;namespace PhoneApp6{ public partial class MainPage : PhoneApplicationPage { private Person p; // 构造函数 public MainPage() { InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); p = new Person() { Name = "Sirius" }; Tb.DataContext = p; } private void Button_Click(object sender, RoutedEventArgs e) { p.Name = DateTime.Now.Millisecond.ToString(); } }}
using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Text;using System.Threading.Tasks;namespace PhoneApp6{ public class Person : INotifyPropertyChanged { private String _name; public String Name { get { return _name; } set { _name = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } } public event PropertyChangedEventHandler PropertyChanged; }}
0 0
- Winphone开发之数据绑定(3)
- Winphone开发之数据绑定(1)
- Winphone开发之数据绑定(2)
- Winphone开发之数据绑定(4)
- Winphone开发之BackgroupWorker
- Winphone开发之IsolatedStorageFile
- WinPhone开发之GeoCoordinateWatcher
- Winphone开发之JSON解析
- Winphone开发之保存设置
- Winphone开发之页面导航
- Winphone开发之资源字典
- 学习WinPhone开发之路
- Winphone开发之下载+显示图片
- Winphone开发之异步任务处理
- Winphone开发之动态加载XAML
- Winphone开发之自定义水印输入框
- Winphone开发之关于XAML Style的若干备注
- winphone 开发环境
- linux重定向简介
- ScrollView下的ListView问题解决
- android 中向手机端安装apk 的时候报出 INSTALL_FAILED_INSUFFICIENT_STORAGE 错误的解决方法
- 浮点数陷阱
- 【36】在O(1)的时间删除链表结点
- Winphone开发之数据绑定(3)
- 判断二叉树是否是平衡二叉树
- leetcode:Binary Tree Maximum Path Sum
- cocos2d-x CCSrollView 源码,可循环的SrollView代码
- nil/Nil/NULL的区别
- Ubuntu下 Hadoop 1.2.1 配置安装
- asp.net SQL Server 存储过程分页及代码调用
- 黑马程序员_【总结】_集合框架知识梳理2
- The Swift Programming Language 中文版