绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue
介绍
背水一战 Windows 10 之 绑定
- 与 Element 绑定
- 与 Indexer 绑定
- TargetNullValue - 当绑定数据为 null 时显示的值
- FallbackValue - 当绑定失败时显示的值
示例
1、演示如何与 Element 绑定
Bind/BindingElement.xaml

<Page
x:Class="Windows10.Bind.BindingElement"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <!--
本例用于演示如何与 Element 绑定,以及 OneTime, OneWay, TwoWay 的区别
--> <!--
OneTime 方式绑定元素
-->
<TextBox Text="{Binding ElementName=txtOneTime, Path=Text, Mode=OneTime}" />
<TextBox Name="txtOneTime" Text="OneTime" Margin="0 10 0 0" /> <!--
OneWay 方式绑定元素(OneWay 是默认方式)
-->
<TextBox Text="{Binding ElementName=txtOneWay, Path=Text, Mode=OneWay}" Margin="0 30 0 0" />
<TextBox Name="txtOneWay" Text="OneWay" Margin="0 10 0 0" /> <!--
TwoWay 方式绑定元素(同时演示一下 Binding 标记的另一种写法,就是直接把 Path 指定的路径放到 Binding 的后面)
-->
<TextBox Text="{Binding Text, ElementName=txtTwoWay, Mode=TwoWay}" Margin="0 30 0 0" />
<TextBox Name="txtTwoWay" Text="TwoWay" Margin="0 10 0 0" /> <!--
TwoWay 方式绑定元素(在 C# 端指定 Binding 对象)
-->
<TextBox Name="textBox1" Margin="0 30 0 0" />
<TextBox Name="textBox2" Text="TwoWay" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>

Bind/BindingElement.xaml.cs

/*
* 演示如何与 Element 绑定
*/ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data; namespace Windows10.Bind
{
public sealed partial class BindingElement : Page
{
public BindingElement()
{
this.InitializeComponent(); this.Loaded += BindingElement_Loaded;
} // 在 C# 端做绑定
private void BindingElement_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
// 实例化 Binding 对象
Binding binding = new Binding()
{
ElementName = nameof(textBox2),
Path = new PropertyPath(nameof(TextBox.Text)),
Mode = BindingMode.TwoWay // 默认是 OneWay 的
}; // 将目标对象的目标属性与指定的 Binding 对象关联
BindingOperations.SetBinding(textBox1, TextBox.TextProperty, binding);
}
}
}

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

<Page
x:Class="Windows10.Bind.BindingIndexer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <!--演示如何绑定集合中的某个元素-->
<TextBlock Name="textBlock" Text="{Binding Path=[3]}" /> <!--演示如何绑定集合中的某个对象的某个属性-->
<TextBlock Name="textBlock2" Text="{Binding Path=[5].Name}" Margin="0 10 0 0" /> <!--演示如何绑定 string 类型的索引器-->
<TextBlock Name="textBlock3" Text="{Binding Path=[webabcd]}" Margin="0 10 0 0" /> <!--演示如何绑定字典表中指定 key 的数据-->
<TextBlock Name="textBlock4" Text="{Binding Path=[hello]}" Margin="0 10 0 0" /> <!--演示如何在 C# 端绑定索引器-->
<TextBox Name="textBox" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>

Bind/BindingIndexer.xaml.cs

/*
* 演示如何与 Indexer 绑定
*/ using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows10.Common; namespace Windows10.Bind
{
public sealed partial class BindingIndexer : Page
{
public BindingIndexer()
{
this.InitializeComponent(); this.Loaded += BindingIndexer_Loaded; BindingDemo();
} private void BindingIndexer_Loaded(object sender, RoutedEventArgs e)
{
// 用于演示如何绑定集合中的某个元素
List<string> list = new List<string>();
for (int i = 0; i < 10; i++)
{
list.Add("索引:" + i.ToString());
}
textBlock.DataContext = list; // 用于演示如何绑定集合中的某个对象的某个属性
textBlock2.DataContext = TestData.GetEmployees(); // 用于演示如何绑定 string 类型的索引器
textBlock3.DataContext = this; // 用于演示如何绑定字典表中指定 key 的数据
Dictionary<string, string> dic = new Dictionary<string, string>() { { "hello", "hello webabcd" } };
textBlock4.DataContext = dic;
} // 自定义一个索引器
public object this[string indexer]
{
get
{
return "string: " + indexer;
}
} // 在 C# 端绑定索引器
private void BindingDemo()
{
textBox.DataContext = this; // 实例化 Binding 对象
Binding binding = new Binding()
{
Path = new PropertyPath("[wanglei]")
}; // 将目标对象的目标属性与指定的 Binding 对象关联
BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding); /*
* 注:经测试在 TextBox 做如上绑定是正常的。但是如果在 TextBlock 做如上绑定则运行时报错 Attempted to read or write protected memory. This is often an indication that other memory is corrupt. 不知道为什么
*/
}
}
}

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

<Page
x:Class="Windows10.Bind.TargetNullValueFallbackValue"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <!--
FallbackValue - 当绑定失败时显示的值
-->
<TextBlock Name="textBlock1" Text="{Binding Path=MyName, FallbackValue='绑定失败时的默认值'}" Margin="5" /> <!--
TargetNullValue - 当绑定数据为 null 时显示的值
-->
<TextBlock Name="textBlock2" Text="{Binding Path=MyName, TargetNullValue='绑定数据的返回值为 null'}" Margin="5" /> </StackPanel>
</Grid>
</Page>

Bind/TargetNullValueFallbackValue.xaml.cs

/*
* 演示 Binding 中的 TargetNullValue 和 FallbackValue 的用法
*/ using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace Windows10.Bind
{
public sealed partial class TargetNullValueFallbackValue : Page
{
public TargetNullValueFallbackValue()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 为 textBlock2 提供数据上下文
textBlock2.DataContext = this; /*
// 实例化 Binding 对象
Binding binding = new Binding()
{
Path = new PropertyPath("Name"),
TargetNullValue = "TargetNullValue",
FallbackValue = "FallbackValue"
}; // 将目标对象的目标属性与指定的 Binding 对象关联
BindingOperations.SetBinding(textBlock2, TextBox.TextProperty, binding);
*/
} public string MyName { get; set; } = null;
}
}
绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue的更多相关文章
- 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue
[源码下载] 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue 作者:weba ...
- 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换
[源码下载] 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数 ...
- 重新想象 Windows 8.1 Store Apps (82) - 绑定: DataContextChanged, TargetNullValue, FallbackValue, UpdateSourceTrigger
[源码下载] 重新想象 Windows 8.1 Store Apps (82) - 绑定: DataContextChanged, TargetNullValue, FallbackValue, Up ...
- 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedFilesVector VirtualizedItemsVector 绑定
[源码下载] 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedF ...
- vue双向绑定的原理及实现双向绑定MVVM源码分析
vue双向绑定的原理及实现双向绑定MVVM源码分析 双向数据绑定的原理是:可以将对象的属性绑定到UI,具体的说,我们有一个对象,该对象有一个name属性,当我们给这个对象name属性赋新值的时候,新值 ...
- Java前期(静态)绑定和后期(动态)绑定
Java前期(静态)绑定和后期(动态)绑定 程序绑定的概念:绑定指的是一个方法的调用与方法所在的类(方法主体)关联起来.对java来说,绑定分为静态绑定和动态绑定:或者叫做前期绑定和后期绑定. 静态绑 ...
- vue.js的一些事件绑定和表单数据双向绑定
知识点: v-on:相当于: 例如:v-on:click==@click ,menthods事件绑定 v-on修饰符可以指定键盘事件 v-model进行表单数据的双向绑定 <template&g ...
- WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)对象绑定
原文:WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)对象绑定 WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件) 上面的 ...
- RabbitMQ中,exchange1绑定exchange2,exchange1和exchange2都绑定queue1,此时消息发送给exchange1,queue1中有几条消息
如题: 存在两个交换器 exchange1,exchange2 存在一个队列 queue1 存在三个绑定关系:exchange1绑定exchange2 ,exchange1绑定queue1,excha ...
随机推荐
- IT菜鸟的第一天
小弟愚钝,被别人影响,打算入IT行业试试水的深浅,俗话说技不压身,多会一种就多一条路子,抱着这种求知的心态我就开始的我在汉企的IT生涯! 第一天无非就是简介,对IT行业的介绍,反正听得我挺懵的,不过介 ...
- 记录mysql的具体操作明细
在MySQL中使用init-connect与binlog来实现用户操作追踪记录 2014-07-28 20:55:38 分类: MySQL 前言:测试环境莫名其妙有几条重要数据被删除了,由于在binl ...
- 阿里云Center OS 6.2 Nginx 配置 SSL/TLS HTTPS配置
1.通过https://www.startssl.com/ 免费SSL证书 STARTSSL 跟VeriSign一样,StartSSL(网址:http://www.startssl.com,公司名:S ...
- Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 用Dart&Henson玩转Activity跳转
用Dart&Henson玩转Activity跳转 Extra是Android标准的组件之间(Activity/Fragment/Service等)传递数据的方式.本文介绍了开源项目Dart的使 ...
- Entity Framework 出现 "此 ObjectContext 实例已释放,不可再用于需要连接的操作" 的错误
原因 Entity的导航属性在View中使用,但是该Entity所在的Context已经在Controller中通过 using 释放掉:但是Entity又具有Deferred Query Evalu ...
- Webmin|Linux管理员远程管理工具
介绍: Webmin is a web-based interface for system administration for Unix. Using any modern web browser ...
- python环境搭建-Pycharm 调整字体大小
- AngularJS指令的详解
指令作为AngularJS中最为重要的部分,所以这个框架本身也是自带了比较多的的指令,但是在开发中,这些指令通常不能满足我们的需要,所以我们也是需要自定义一些指令的.指令是我们用来扩展浏览器能力的技术 ...
- SpringMVC学习--拦截器
简介 Spring Web MVC 的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理. 拦截器定义 定义拦截器,实现HandlerInterceptor接 ...