这篇文章主要介绍怎么自定义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:PhoneApplicationPagex: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:PhoneApplicationPagex: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;}
}

Winphone开发之数据绑定(3)相关推荐

  1. WinPhone开发阶段总结

    首先,这篇文章基本没什么技术含量,它主要是我对最近业余时间的WinPhone开发工作的一个总结.而且以我以往的学习经验来说,这篇文章很可能对读者中的大部分人都毫无用处,因为所谓学习方法因人而异,这我深 ...

  2. winphone开发环境配置

    环境:操作系统win7 要进行winphone开发,必须进行一些环境的配置.下面是我的一些配置总结. 1.操作系统 winphone开发仅仅能在win8下开发.所以首先得安装win8.能够使用nt6 ...

  3. 入门微信小程序开发(三)数据绑定的几种用法

    一.数据绑定 与书写HTML页面一样,小程序页面也只能通过多写多练提升.在构建完页面后就出现了一个问题,我们该如何获取组件元素让数据与之关联呢? 在网页开发中,我们同样使用JS操作DOM,包括数据渲染 ...

  4. 微信小程序开发之六 —— 数据绑定与列表渲染

    文章目录 学习前后 数据分离 数据类型 组件属性数据绑定 渲染对象类型数据 列表渲染 wx:for wx:key 渲染一下电影列表 学习前后 上一篇:微信小程序开发之五 -- 体验WeUI 下一篇:微 ...

  5. 微信小程序开发2——数据绑定、控制属性

    数据绑定 index.wxml <!--index.wxml--> <view class="container"><!-- {{ }} 小胡子语法 ...

  6. 【万里征程——Windows App开发】数据绑定——简单示例、更改通知、数据转换...

    简单的数据绑定示例 相比于理论,我更倾向于从实践中开始博客,尤其是对于数据绑定.那么,我们先来看看几个简单的例子. 1.数据绑定到TextBox 我们依旧使用前面的闹钟类来开始.在下面的代码中,我们有 ...

  7. 微信小程序开发的数据绑定和事件绑定

    一.数据绑定 1.定义数据 在页面对应的 .js(或 ts)文件中,把数据定义到 data 对象中即可: 2.Mustache 语法的格式(渲染数据) 把 data 中的数据绑定到页面中渲染,使用 M ...

  8. WinPhone开发疑问与解答

    疑问:怎样获取开发者许可证 打开VS2012时,怎么在没有取得开发者许可证之前,屏蔽/跳过弹出的窗体"获取Windows8开发者许可证 你需要具有开发者许可证才能开发适用于......&qu ...

  9. WinPhone 开发(1)-----在 XAML 页面之间浏览和数据的传递、保留以及恢复

    使用控件HyperlinkButton实现XAML页面之间的浏览,使用方法:设置HyperlinkButton的NavigateUri为"/ProjectName;component/fol ...

最新文章

  1. The Six Best Practices(4~6)
  2. 极客广州——EOS Asia郭达峰担任SegmentFault思否黑客马拉松技术顾问
  3. 算法导论——计数排序
  4. Python学习之路—2018/6/20
  5. Request.ServerVariables的详细应用(转)
  6. 点歌软件测试自学,实际歌唱对比测试
  7. mamp安装php扩展,向MAMP添加GMP PHP扩展
  8. 帆软高级函数应用之其他函数
  9. CCAA 信息安全管理体系 考试大纲及重点题目记忆
  10. 霍尔探头对高斯计测量的影响
  11. GPS 相关知识科普
  12. 时间管理-要抽专门的时间去做那些重要但不紧急的事情
  13. A goal-driven tree-structured neural model for math word problems论文阅读
  14. Java语言知识大盘点(期末总复习)三
  15. WebSocket通讯C#实例
  16. 学习 瑞吉外卖项目——总结
  17. 大数据发展规划及技术详解
  18. docx4j word转pdf解决中文乱码问题(包括宋体(正文))
  19. Android支持百分比布局
  20. 3d打印光固化好还是热固化好_光固化3D打印机定制

热门文章

  1. 吴恩达-深度学习-各个变量的求导
  2. 开源圈又一瓜!自研软件与开源雷同疑似抄袭。。。
  3. “无印良品”的品牌困局:小众文化与全球化扩张
  4. 数据库顶会VLDB论文解读:阿里巴巴数据库智能参数优化的创新与实践
  5. Java8之熟透Lambda表达式
  6. 《Attention Is All You Need》算法详解
  7. i8042错误linux7,I8042.c: No controller found
  8. 校园跑腿|前后端分离跑腿项目Springboot+vue
  9. 【代码阅读】DeepSC--preprocess_text.py
  10. 饿了吗 系统_跑外卖的奖励很高么,真的是这样吗?