背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue
作者:webabcd
介绍
背水一战 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" /> <!--
TwoWay 方式绑定元素(在 C# 端指定 Binding 对象的方式二)
-->
<TextBox Name="textBox3" Margin="0 30 0 0" />
<TextBox Name="textBox4" 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(); SetBindingDemo1();
SetBindingDemo2();
} // 在 C# 端做绑定(方式一)
private void SetBindingDemo1()
{
// 实例化 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);
} // 在 C# 端做绑定(方式二)
private void SetBindingDemo2()
{
// 实例化 Binding 对象
Binding binding = new Binding()
{
ElementName = nameof(textBox4),
Path = new PropertyPath(nameof(TextBox.Text)),
Mode = BindingMode.TwoWay // 默认是 OneWay 的
}; // 将目标对象的目标属性与指定的 Binding 对象关联
textBox3.SetBinding(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 = ; i < ; 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;
}
}
OK
[源码下载]
背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue的更多相关文章
- 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧
[源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...
- 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合
[源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...
- 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换
[源码下载] 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 作者:webabcd 介 ...
- 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定
[源码下载] 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定 作者:we ...
- 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout
[源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...
- 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox
[源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...
- 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter
[源码下载] 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter 作者:weba ...
- 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton
[源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...
- 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null
[源码下载] 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null 作者:webabcd 介绍背水一战 Windows 10 之 XAML ...
随机推荐
- Entity Framework与ADO.NET批量插入数据性能测试
Entity Framework是.NET平台下的一种简单易用的ORM框架,它既便于Domain Model和持久层的OO设计,也提高了代码的可维护性.但在使用中发现,有几类业务场景是EF不太擅长的, ...
- InstallShield2013 error 6109
InstallShield在没添加打包文件时是可以编译生成安装包的,在增加打包文件之后就报以下错误: ISEXP : error : -6109: Internal build error ISEXP ...
- C语言 · 十六进制转十进制
问题描述 从键盘输入一个不超过8位的正的十六进制数字符串,将它转换为正的十进制数后输出. 注:十六进制数中的10~15分别用大写的英文字母A.B.C.D.E.F表示. 样例输入 FFFF 样例输出 6 ...
- bootstrap-popover的配置与灵活应用
首先罗列一下配置参数: 1.animation true/false 是否动画 2.placement 'right'/'left'/top/bottom/function(){return 'rig ...
- jQuery 2.0.3 源码分析Sizzle引擎 - 超级匹配
声明:本文为原创文章,如需转载,请注明来源并保留原文链接Aaron,谢谢! 通过Expr.find[ type ]我们找出选择器最右边的最终seed种子合集 通过Sizzle.compile函数编译器 ...
- 控制对话框风格的activity的显示大小与位置
项目开发的需要,因为到现在项目接近完工,用户提出对条件筛选方式进行修改,为做到最小的改动实现用户的需求,各种百度,对于对话框风格大家普遍使用PopupWindow,但由于之前开发设计时使用的是acti ...
- .Net 转战 Android 4.4 日常笔记(10)--PullToRefresh下拉刷新使用
下拉刷新很多地方都用到了,新浪微博,微信,百度新闻 这里我们使用一个开源的库叫:PullToRefresh 开源地址:https://github.com/chenyoca/pull-to-refre ...
- android5.0联系人 sort_key改成phonebook_label
项目中用到了联系人根据字母排序,在android4.0手机上是可以的,但是在android4.4以上的手机排序是乱的,一般字母排序都是根据sort_key这个拼音进行排序,而android5.0这个字 ...
- 优化javaScript代码,提高执行效率
今天看完书,总结了一下可以如何优化 JavaScript . 1.合并js文件 为优化性能,可以把多个js文件(css文件也可以)合并成极少数大文件.跟十个5k的js文件相比,合并成一个50k的文件更 ...
- golang操作文件
1.读取文件信息: /* 读取文件信息 */ func readFile(path string) string { fi, err := os.Open(path) if err != nil { ...