博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧...
阅读量:6962 次
发布时间:2019-06-27

本文共 6773 字,大约阅读时间需要 22 分钟。

原文:

背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧

作者:
介绍
背水一战 Windows 10 之 绑定

  • x:Bind 绑定
  • x:Bind 绑定之 x:Phase
  • 使用绑定过程中的一些技巧

示例
1、演示 x:Bind 绑定的相关知识点
Bind/BindDemo.xaml

Bind/BindDemo.xaml.cs

/* * 演示 x:Bind 绑定的相关知识点 */using System;using System.Collections.ObjectModel;using System.ComponentModel;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows10.Common;namespace Windows10.Bind{    // x:Bind 的数据上下文就是它所属的 Page 或 UserControl    public sealed partial class BindDemo : Page    {        public BindDemo()        {            this.InitializeComponent();        }        // 事件绑定到方法,无参数        private void EventBindNoArgs()        {            CurrentEmployee.Name = "wanglei" + new Random().Next(1000, 10000).ToString();        }        // 事件绑定到方法,参数与对应的事件的参数相同        private void EventBindRegularArgs(object sender, RoutedEventArgs e)        {            CurrentEmployee.Name = "wanglei" + new Random().Next(1000, 10000).ToString();        }        // 事件绑定到方法,参数与对应的事件的参数相同,但是其中的事件参数为 object 类型        private void EventBindBaseArgs(object sender, object e)        {            CurrentEmployee.Name = "wanglei" + new Random().Next(1000, 10000).ToString();        }        public Employee CurrentEmployee { get; set; } = new Employee() { Name = "wanglei", Age = 36, IsMale = true };        public ObservableCollection
AllEmployees { get; set; } = TestData.GetEmployees(5); }}

2、演示 x:Bind 绑定之 x:Phase 的相关知识点
Bind/PhaseDemo.xaml

Bind/PhaseDemo.xaml.cs

/* * 演示 x:Bind 绑定之 x:Phase 的相关知识点 */using System.Collections.ObjectModel;using System.Threading;using Windows.UI.Xaml.Controls;using Windows10.Common;namespace Windows10.Bind{    public sealed partial class PhaseDemo : Page    {        public PhaseDemo()        {            this.InitializeComponent();        }        // 用于人为减慢每阶段的显示速度,以便演示        private void gridView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)        {            AutoResetEvent h = new AutoResetEvent(false);            h.WaitOne(1);            uint phase = args.Phase;            if (phase < 10)                args.RegisterUpdateCallback(gridView_ContainerContentChanging);        }        // 数据源        public ObservableCollection
AllEmployees { get; set; } = TestData.GetEmployees(1000); }}

3、演示使用绑定过程中的一些技巧
Bind/Tips.xaml

Bind/Tips.xaml.cs

/* * 用于演示使用绑定过程中的一些技巧 * * 在这里插一句: * 在 xaml 使用的 {Binding}, {x:Bind}, {StaticResource} 之类的这种带大括号的语法被称为标记扩展(Markup Extension),在 uwp 中无法开发自定义标记扩展(但是在 wpf 中是可以的) */using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Data;using Windows10.Common;namespace Windows10.Bind{    public sealed partial class Tips : Page    {        public Tips()        {            this.InitializeComponent();            this.Loaded += Tips_Loaded;        }        private void Tips_Loaded(object sender, RoutedEventArgs e)        {            BindingAttachedProperty();        }        // 在 CodeBehind 端绑定附加属性        private void BindingAttachedProperty()        {            Binding binding = new Binding()            {                Path = new PropertyPath("(Grid.Row)"), // 注意要有括号,另外在 CodeBehind 端绑定自定义附加属性暂时没有成功                Source = textBox3            };            BindingOperations.SetBinding(textBox3, TextBox.TextProperty, binding);        }        // 通过 x:Bind 绑定时,要做转换        public object CurrentEmployee { get; set; } = new Employee() { Name = "wanglei", Age = 36, IsMale = true };        public string MyName { get; set; } = "webabcd";    }    ///     /// 用于附加属性的演示    ///     public class MyAttachedProperty    {        // 获取附加属性        public static string GetSubTitle(DependencyObject obj)        {            return (string)obj.GetValue(SubTitleProperty);        }        // 设置附加属性        public static void SetSubTitle(DependencyObject obj, string value)        {            obj.SetValue(SubTitleProperty, value);        }        // 注册一个附加属性        public static readonly DependencyProperty SubTitleProperty =            DependencyProperty.RegisterAttached(                "SubTitle", // 附加属性的名称                typeof(string), // 附加属性的数据类型                typeof(MyAttachedProperty), // 附加属性所属的类                new PropertyMetadata("", PropertyMetadataCallback)); // 指定附加属性的默认值,以及值发生改变时所调用的方法        private static void PropertyMetadataCallback(DependencyObject sender, DependencyPropertyChangedEventArgs args)        {            object newValue = args.NewValue; // 发生改变之后的值            object oldValue = args.OldValue; // 发生改变之前的值        }    }}

OK

转载地址:http://unzsl.baihongyu.com/

你可能感兴趣的文章
全面降低windows系统的安全隐患(一)[Web安全大家谈]
查看>>
MySQL 可以用localhost 连接,但不能用IP连接的问题
查看>>
Linux命令之chkconfig
查看>>
单臂路由的实现
查看>>
还有人不认识通讯诈骗,短信验证码带你认识一下
查看>>
用maven时出现,报错 miss 一些包,但是发现项目里已经引入了,但还是报错
查看>>
大快DKhadoop大数据处理平台详解
查看>>
摄影菜鸟使用的相机镜头术语大全分享
查看>>
XenServer部署系列之06——网络配置
查看>>
Python黑科技:50行代码运用Python+OpenCV实现人脸追踪+详细教程+快速入门+图像识...
查看>>
软件测试质量和效率评价之我见
查看>>
kloxo增加了域名,怎么不能访问?如何重启web服务?
查看>>
国学题库整理
查看>>
jquery chosen 插件 动态设置+更新选项值
查看>>
求最大值及其下标
查看>>
战力会议1
查看>>
水印情缘。。。
查看>>
为什么重写equals一定要重写hashCode?
查看>>
HDU Problem 4006 The kth great number 【队列】
查看>>
win8阉割版中文输入法
查看>>