[源码下载]

背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue

作者:webabcd

介绍
背水一战 Windows 10 之 绑定

  • 与 Element 绑定
  • 与 Indexer 绑定
  • TargetNullValue - 当绑定数据为 null 时显示的值
  • FallbackValue - 当绑定失败时显示的值

示例
1、演示如何与 Element 绑定
Bind/BindingElement.xaml

  1. <Page
  2. x:Class="Windows10.Bind.BindingElement"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Bind"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <!--
  14. 本例用于演示如何与 Element 绑定,以及 OneTime, OneWay, TwoWay 的区别
  15. -->
  16.  
  17. <!--
  18. OneTime 方式绑定元素
  19. -->
  20. <TextBox Text="{Binding ElementName=txtOneTime, Path=Text, Mode=OneTime}" />
  21. <TextBox Name="txtOneTime" Text="OneTime" Margin="0 10 0 0" />
  22.  
  23. <!--
  24. OneWay 方式绑定元素(OneWay 是默认方式)
  25. -->
  26. <TextBox Text="{Binding ElementName=txtOneWay, Path=Text, Mode=OneWay}" Margin="0 30 0 0" />
  27. <TextBox Name="txtOneWay" Text="OneWay" Margin="0 10 0 0" />
  28.  
  29. <!--
  30. TwoWay 方式绑定元素(同时演示一下 Binding 标记的另一种写法,就是直接把 Path 指定的路径放到 Binding 的后面)
  31. -->
  32. <TextBox Text="{Binding Text, ElementName=txtTwoWay, Mode=TwoWay}" Margin="0 30 0 0" />
  33. <TextBox Name="txtTwoWay" Text="TwoWay" Margin="0 10 0 0" />
  34.  
  35. <!--
  36. TwoWay 方式绑定元素(在 C# 端指定 Binding 对象的方式一)
  37. -->
  38. <TextBox Name="textBox1" Margin="0 30 0 0" />
  39. <TextBox Name="textBox2" Text="TwoWay" Margin="0 10 0 0" />
  40.  
  41. <!--
  42. TwoWay 方式绑定元素(在 C# 端指定 Binding 对象的方式二)
  43. -->
  44. <TextBox Name="textBox3" Margin="0 30 0 0" />
  45. <TextBox Name="textBox4" Text="TwoWay" Margin="0 10 0 0" />
  46.  
  47. </StackPanel>
  48. </Grid>
  49. </Page>

Bind/BindingElement.xaml.cs

  1. /*
  2. * 演示如何与 Element 绑定
  3. */
  4.  
  5. using Windows.UI.Xaml;
  6. using Windows.UI.Xaml.Controls;
  7. using Windows.UI.Xaml.Data;
  8.  
  9. namespace Windows10.Bind
  10. {
  11. public sealed partial class BindingElement : Page
  12. {
  13. public BindingElement()
  14. {
  15. this.InitializeComponent();
  16.  
  17. SetBindingDemo1();
  18. SetBindingDemo2();
  19. }
  20.  
  21. // 在 C# 端做绑定(方式一)
  22. private void SetBindingDemo1()
  23. {
  24. // 实例化 Binding 对象
  25. Binding binding = new Binding()
  26. {
  27. ElementName = nameof(textBox2),
  28. Path = new PropertyPath(nameof(TextBox.Text)),
  29. Mode = BindingMode.TwoWay // 默认是 OneWay 的
  30. };
  31.  
  32. // 将目标对象的目标属性与指定的 Binding 对象关联
  33. BindingOperations.SetBinding(textBox1, TextBox.TextProperty, binding);
  34. }
  35.  
  36. // 在 C# 端做绑定(方式二)
  37. private void SetBindingDemo2()
  38. {
  39. // 实例化 Binding 对象
  40. Binding binding = new Binding()
  41. {
  42. ElementName = nameof(textBox4),
  43. Path = new PropertyPath(nameof(TextBox.Text)),
  44. Mode = BindingMode.TwoWay // 默认是 OneWay 的
  45. };
  46.  
  47. // 将目标对象的目标属性与指定的 Binding 对象关联
  48. textBox3.SetBinding(TextBox.TextProperty, binding);
  49. }
  50. }
  51. }

2、演示如何与 Indexer 绑定
Bind/BindingIndexer.xaml

  1. <Page
  2. x:Class="Windows10.Bind.BindingIndexer"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Bind"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <!--演示如何绑定集合中的某个元素-->
  14. <TextBlock Name="textBlock" Text="{Binding Path=[3]}" />
  15.  
  16. <!--演示如何绑定集合中的某个对象的某个属性-->
  17. <TextBlock Name="textBlock2" Text="{Binding Path=[5].Name}" Margin="0 10 0 0" />
  18.  
  19. <!--演示如何绑定 string 类型的索引器-->
  20. <TextBlock Name="textBlock3" Text="{Binding Path=[webabcd]}" Margin="0 10 0 0" />
  21.  
  22. <!--演示如何绑定字典表中指定 key 的数据-->
  23. <TextBlock Name="textBlock4" Text="{Binding Path=[hello]}" Margin="0 10 0 0" />
  24.  
  25. <!--演示如何在 C# 端绑定索引器-->
  26. <TextBox Name="textBox" Margin="0 10 0 0" />
  27.  
  28. </StackPanel>
  29. </Grid>
  30. </Page>

Bind/BindingIndexer.xaml.cs

  1. /*
  2. * 演示如何与 Indexer 绑定
  3. */
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using Windows.UI.Xaml;
  8. using Windows.UI.Xaml.Controls;
  9. using Windows.UI.Xaml.Data;
  10. using Windows10.Common;
  11.  
  12. namespace Windows10.Bind
  13. {
  14. public sealed partial class BindingIndexer : Page
  15. {
  16. public BindingIndexer()
  17. {
  18. this.InitializeComponent();
  19.  
  20. this.Loaded += BindingIndexer_Loaded;
  21.  
  22. BindingDemo();
  23. }
  24.  
  25. private void BindingIndexer_Loaded(object sender, RoutedEventArgs e)
  26. {
  27. // 用于演示如何绑定集合中的某个元素
  28. List<string> list = new List<string>();
  29. for (int i = ; i < ; i++)
  30. {
  31. list.Add("索引:" + i.ToString());
  32. }
  33. textBlock.DataContext = list;
  34.  
  35. // 用于演示如何绑定集合中的某个对象的某个属性
  36. textBlock2.DataContext = TestData.GetEmployees();
  37.  
  38. // 用于演示如何绑定 string 类型的索引器
  39. textBlock3.DataContext = this;
  40.  
  41. // 用于演示如何绑定字典表中指定 key 的数据
  42. Dictionary<string, string> dic = new Dictionary<string, string>() { { "hello", "hello webabcd" } };
  43. textBlock4.DataContext = dic;
  44. }
  45.  
  46. // 自定义一个索引器
  47. public object this[string indexer]
  48. {
  49. get
  50. {
  51. return "string: " + indexer;
  52. }
  53. }
  54.  
  55. // 在 C# 端绑定索引器
  56. private void BindingDemo()
  57. {
  58. textBox.DataContext = this;
  59.  
  60. // 实例化 Binding 对象
  61. Binding binding = new Binding()
  62. {
  63. Path = new PropertyPath("[wanglei]")
  64. };
  65.  
  66. // 将目标对象的目标属性与指定的 Binding 对象关联
  67. BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding);
  68.  
  69. /*
  70. * 注:经测试在 TextBox 做如上绑定是正常的。但是如果在 TextBlock 做如上绑定则运行时报错 Attempted to read or write protected memory. This is often an indication that other memory is corrupt. 不知道为什么
  71. */
  72. }
  73. }
  74. }

3、演示 Binding 中的 TargetNullValue 和 FallbackValue 的用法
Bind/TargetNullValueFallbackValue.xaml

  1. <Page
  2. x:Class="Windows10.Bind.TargetNullValueFallbackValue"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Bind"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11.  
  12. <StackPanel Margin="10 0 10 10">
  13.  
  14. <!--
  15. FallbackValue - 当绑定失败时显示的值
  16. -->
  17. <TextBlock Name="textBlock1" Text="{Binding Path=MyName, FallbackValue='绑定失败时的默认值'}" Margin="5" />
  18.  
  19. <!--
  20. TargetNullValue - 当绑定数据为 null 时显示的值
  21. -->
  22. <TextBlock Name="textBlock2" Text="{Binding Path=MyName, TargetNullValue='绑定数据的返回值为 null'}" Margin="5" />
  23.  
  24. </StackPanel>
  25. </Grid>
  26. </Page>

Bind/TargetNullValueFallbackValue.xaml.cs

  1. /*
  2. * 演示 Binding 中的 TargetNullValue 和 FallbackValue 的用法
  3. */
  4.  
  5. using Windows.UI.Xaml.Controls;
  6. using Windows.UI.Xaml.Navigation;
  7.  
  8. namespace Windows10.Bind
  9. {
  10. public sealed partial class TargetNullValueFallbackValue : Page
  11. {
  12. public TargetNullValueFallbackValue()
  13. {
  14. this.InitializeComponent();
  15. }
  16.  
  17. protected override void OnNavigatedTo(NavigationEventArgs e)
  18. {
  19. // 为 textBlock2 提供数据上下文
  20. textBlock2.DataContext = this;
  21.  
  22. /*
  23. // 实例化 Binding 对象
  24. Binding binding = new Binding()
  25. {
  26. Path = new PropertyPath("Name"),
  27. TargetNullValue = "TargetNullValue",
  28. FallbackValue = "FallbackValue"
  29. };
  30.  
  31. // 将目标对象的目标属性与指定的 Binding 对象关联
  32. BindingOperations.SetBinding(textBlock2, TextBox.TextProperty, binding);
  33. */
  34. }
  35.  
  36. public string MyName { get; set; } = null;
  37. }
  38. }

OK
[源码下载]

背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue的更多相关文章

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

    [源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...

  2. 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合

    [源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...

  3. 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换

    [源码下载] 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 作者:webabcd 介 ...

  4. 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定

    [源码下载] 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定 作者:we ...

  5. 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout

    [源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...

  6. 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox

    [源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...

  7. 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter

    [源码下载] 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter 作者:weba ...

  8. 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

    [源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...

  9. 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null

    [源码下载] 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null 作者:webabcd 介绍背水一战 Windows 10 之 XAML ...

随机推荐

  1. JavaScript面向对象之我见

    序言 在JavaScript的大世界里讨论面向对象,都要提到两点:1.JavaScript是一门基于原型的面向对象语言 2.模拟类语言的面向对象方式.对于为什么要模拟类语言的面向对象,我个人认为:某些 ...

  2. lua中清空目录和递归创建目录

    lua中的 lfs.mkdir lfs.rmdir只能针对单个目录,且lfs.rmdir不能清空文件夹 于是我想到了使用os.execute 递归创建目录如下os.execute("mkdi ...

  3. Mongodb 学习笔记

    Mongo DB NoSql简介 MongoDB简介 在Windows平台下安装Mongo Mongo DB官方文档 MongoDB基本命令 MongoDB的默认安装路径为 C:\Program Fi ...

  4. Azure PowerShell (4) 使用PowerShell管理多个订阅

    <Windows Azure Platform 系列文章目录> 笔者手上有两个Azure账户. - Azure Global (windowsazure.com)账户.有两个订阅. - 世 ...

  5. 《JS修炼之道》—— 读后总结

    本篇是基于<JS修炼之道>的记录性与总结性的文章,这本书从多种框架的角度,讲述了JS开发中的一些实用技巧. 比如Prototype,JQuery,Mootools,YUI,Dojo,Ext ...

  6. PHP 数据访问

    如何连接 1.造连接对象 $db= new MySQLi("localhost","root","123","mydb" ...

  7. android studio和eclipse中如何获取sha1值

    首先如果是eclipse的话, 直接打开eclipse开发工具 那么接下来问题来了,现在很多开发者都已经从es转型到as开发工具了, 在android studio上没有直接提供这个GUI界面让我们去 ...

  8. GUID简介

    GUID (全局唯一标识符) 编辑 全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.GUID主要用于在拥有多个节点. ...

  9. php 操作数组(合并,拆分,追加,查找,删除等)(转载)

    1. 合并数组 array_merge()函数将数组合并到一起,返回一个联合的数组.所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次迫加.其形式为: array array_merg ...

  10. WPF绘制折线

    WPF后台绘制折线,填充到一个GRID下 private void btnPreview_Click(object sender, RoutedEventArgs e) { GridImg.Child ...