绑定: 与 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 ...
随机推荐
- Flex布局教程及属性速查
一.Flex布局介绍 伸缩盒模型(flexbox)是一个新的盒子模型,意为"弹性布局",用来为盒状模型提供最大的灵活性,主要优化了UI布局.Flexbox的功能主要包手:简单使用一 ...
- 虚拟机安装的UBUNTU怎么全屏
虚拟机下面安装了ubuntu系统,显示的屏幕只有那么一小块儿,不知道如何才能全屏,那么如何全屏呢?且看下面经验. 步骤阅读 百度经验:jingyan.baidu.com 方法/步骤 1 打开虚拟机 ...
- [No00000D]word如何批量删除超链接 怎么去掉网址保留文字
1.删除超链接的文字及网址 这种情况是想把带有网址的文字统统删掉,文字和网址一概不留. 首先在word界面按下ALT+F9(在所有的域代码及其结果间进行切换.),超链接文本会被转换成代码的样式. 例如 ...
- tyvj[1089]smrtfun
描述 现有N个物品,第i个物品有两个属性A_i和B_i.在其中选取若干个物品,使得sum{A_i + B_i}最大,同时sum{A_i},sum{B_i}均非负(sum{}表示求和). 输入格式 ...
- gitlab配置邮件通知功能操作记录
之前已经介绍了gitlab的部署http://www.cnblogs.com/kevingrace/p/5651402.html但是没有配置邮箱通知功能,今天这里介绍下gitlab安装后的邮箱配置操作 ...
- 关于codeMirror插件使用的一个坑
codeMirror插件可以做语法高亮渲染,但它操作过程是这样的:先从 textarea中读取值放到codemirror动态生成的div中,根据textarea中的换行个数确定行数,根据正则表达来高亮 ...
- 启动Eclipse后卡在 android sdk content loader 的解决办法
Make sure that eclipse is not active. If it is active kill eclipse from the processes tab of the tas ...
- 02Spring_Ioc和DI介绍
什么是IOC? IoC: 控制反转, 解决程序对象紧密耦合问题(工厂+反射+ 配置文件), 将程序中原来构造对象的权限,交给IoC容器来构造,当程序需要对象,找IoC容器获取.
- Collections和Arrays常用方法
Collections:常见方法: 1, 对list进行二分查找: 前提该集合一定要有序. int binarySearch(list,key); //必须根据元素自然顺序对列表进行升级排序 //要求 ...
- Android子线程真的不能更新UI么
Android单线程模型是这样描述的: Android UI操作并不是线程安全的,并且这些操作必须在UI线程执行 如果在其它线程访问UI线程,Android提供了以下的方式: Activity.run ...