重新想象 Windows 8.1 Store Apps (82) - 绑定: DataContextChanged, TargetNullValue, FallbackValue, UpdateSourceTrigger
作者:webabcd
介绍
重新想象 Windows 8.1 Store Apps 之绑定
- DataContextChanged - FrameworkElement 的 DataContext 发生变化时触发的事件
- TargetNullValue - 当绑定数据为 null 时所需要显示的值
- FallbackValue - 当绑定失败(无法返回值)的时候所需要显示的值
- UpdateSourceTrigger - UI 上数据更新的触发方式
示例
1、演示 DataContextChanged 的应用
DataContextChanged.xaml
<Page
x:Class="Windows81.Binding.DataContextChanged"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows81.Binding"
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="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button x:Name="btnChange" Content="改变数据上下文" Click="btnChange_Click" Margin="0 10 0 0" /> <ListBox x:Name="listBox" ItemsSource="{Binding}" DataContextChanged="listBox_DataContextChanged" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>
DataContextChanged.xaml.cs
/*
* DataContextChanged - FrameworkElement 的 DataContext 发生变化时触发的事件
*
*
* 关于绑定的基础请参见:
* http://www.cnblogs.com/webabcd/archive/2013/08/19/3267115.html
* http://www.cnblogs.com/webabcd/archive/2013/08/22/3274099.html
* http://www.cnblogs.com/webabcd/archive/2013/08/26/3281822.html
* http://www.cnblogs.com/webabcd/archive/2013/08/29/3288304.html
*/ using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows81.Binding
{
public sealed partial class DataContextChanged : Page
{
public DataContextChanged()
{
this.InitializeComponent();
this.Loaded += new RoutedEventHandler(DataContextChanged_Loaded);
} void DataContextChanged_Loaded(object sender, RoutedEventArgs e)
{
// 指定数据上下文
listBox.DataContext = new List<string> { "a", "b", "c" };
} private void btnChange_Click(object sender, RoutedEventArgs e)
{
// 修改数据上下文
listBox.DataContext = new List<string> { "a", "b", new Random().Next(, ).ToString().PadLeft(, '') };
} private void listBox_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
/*
* FrameworkElement.DataContextChanged - 数据上下文发生改变后所触发的事件
*/ // 数据上下文发生改变后
lblMsg.Text = "数据源发生改变:" + DateTime.Now.ToString("hh:mm:ss"); }
}
}
2、演示 TargetNullValue 和 FallbackValue 的应用
TargetNullValueFallbackValue.xaml
<Page
x:Class="Windows81.Binding.TargetNullValueFallbackValue"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows81.Binding"
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 Name="stackPanel" Margin="120 0 0 0"> <!--
FallbackValue - 当绑定失败(无法返回值)的时候所需要显示的值
-->
<TextBlock FontSize="14.667" Text="{Binding Path=xxx, FallbackValue='绑定失败时的默认值'}" /> <!--
TargetNullValue - 当绑定数据为 null 时所需要显示的值
-->
<TextBlock FontSize="14.667" Text="{Binding Path=Name, TargetNullValue='绑定返回值为 null'}" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>
TargetNullValueFallbackValue.xaml.cs
/*
* TargetNullValue - 当绑定数据为 null 时所需要显示的值
* FallbackValue - 当绑定失败(无法返回值)的时候所需要显示的值
*
*
* 关于绑定的基础请参见:
* http://www.cnblogs.com/webabcd/archive/2013/08/19/3267115.html
* http://www.cnblogs.com/webabcd/archive/2013/08/22/3274099.html
* http://www.cnblogs.com/webabcd/archive/2013/08/26/3281822.html
* http://www.cnblogs.com/webabcd/archive/2013/08/29/3288304.html
*/ using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace Windows81.Binding
{
public sealed partial class TargetNullValueFallbackValue : Page
{
public TargetNullValueFallbackValue()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
stackPanel.DataContext = new TargetNullValueTest { Name = null };
}
} public sealed class TargetNullValueTest
{
public string Name { get; set; }
}
}
3、演示 UpdateSourceTrigger 的应用
UpdateSourceTrigger.xaml
<Page
x:Class="Windows81.Binding.UpdateSourceTrigger"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows81.Binding"
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 Name="stackPanel" Margin="120 0 0 0"> <TextBlock Name="lblMsg" /> <!--
UpdateSourceTrigger - UI 上数据更新的触发方式
Default - 失去焦点后触发
PropertyChanged - 属性值发生改变后触发
Explicit - 需要通过 BindingExpression.UpdateSource() 显示触发
--> <TextBox Text="{Binding Text, Mode=TwoWay, ElementName=lblMsg, UpdateSourceTrigger=Default}" Margin="0 10 0 0" />
<TextBox Text="{Binding Text, Mode=TwoWay, ElementName=lblMsg, UpdateSourceTrigger=PropertyChanged}" Margin="0 10 0 0" />
<TextBox Name="txtExplicit" Text="{Binding Text, Mode=TwoWay, ElementName=lblMsg, UpdateSourceTrigger=Explicit}" Margin="0 10 0 0" /> <Button Name="btnBinding" Content="显示触发更新" Click="btnBinding_Click" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>
UpdateSourceTrigger.xaml.cs
/*
* UpdateSourceTrigger - UI 上数据更新的触发方式
* Default - 失去焦点后触发
* PropertyChanged - 属性值发生改变后触发
* Explicit - 需要通过 BindingExpression.UpdateSource() 显示触发
*
*
* 关于绑定的基础请参见:
* http://www.cnblogs.com/webabcd/archive/2013/08/19/3267115.html
* http://www.cnblogs.com/webabcd/archive/2013/08/22/3274099.html
* http://www.cnblogs.com/webabcd/archive/2013/08/26/3281822.html
* http://www.cnblogs.com/webabcd/archive/2013/08/29/3288304.html
*/ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data; namespace Windows81.Binding
{
public sealed partial class UpdateSourceTrigger : Page
{
public UpdateSourceTrigger()
{
this.InitializeComponent();
} private void btnBinding_Click(object sender, RoutedEventArgs e)
{
// 显示触发 txtExplicit 的数据更新
BindingExpression be = txtExplicit.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
}
}
}
OK
[源码下载]
重新想象 Windows 8.1 Store Apps (82) - 绑定: DataContextChanged, TargetNullValue, FallbackValue, UpdateSourceTrigger的更多相关文章
- 重新想象 Windows 8.1 Store Apps 系列文章索引
[源码下载] [重新想象 Windows 8 Store Apps 系列文章] 重新想象 Windows 8.1 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...
- 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图
[源码下载] 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Co ...
- 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar
[源码下载] 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...
- 重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker
[源码下载] 重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker 作者:webabcd 介绍重新想象 Windows 8.1 ...
- 重新想象 Windows 8.1 Store Apps (74) - 新增控件: Flyout, MenuFlyout, SettingsFlyout
[源码下载] 重新想象 Windows 8.1 Store Apps (74) - 新增控件: Flyout, MenuFlyout, SettingsFlyout 作者:webabcd 介绍重新想象 ...
- 重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink
[源码下载] 重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink 作者:webabcd 介绍重新想象 Windows 8.1 Store A ...
- 重新想象 Windows 8.1 Store Apps (76) - 新增控件: SearchBox
[源码下载] 重新想象 Windows 8.1 Store Apps (76) - 新增控件: SearchBox 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之 ...
- 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性
[源码下载] 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件 ...
- 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup
[源码下载] 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup 作者:webabcd 介绍重新想象 Wind ...
随机推荐
- ios中将事件添加到系统日历
- (void)saveEvent:(id)sender { //事件市场 EKEventStore *eventStore = [[EKEventStore alloc] init]; //6.0及 ...
- Jdbc如何从PostgreSql读取海量数据?PostgreSql源代码分析纪录
前言: 最近做数据同步,需要从PostgreSql获取数据,发现一旦数据比较多,那么读取的速度非常慢,并且内存占用特别多&GC不掉. 代码样例: 为了方便讲解,下面写了事例代码,从b2c_or ...
- AX 与Citrix打印机问题
国外文章,抄个链接,备查 http://blogs.msdn.com/b/axsupport/archive/2010/07/06/ax-2009-citrix-amp-terminal-server ...
- 在自己的对象里实现IEnumerator和IEnumerable
平时工作中我们经常用foreach来迭代一个集合.比如 foreach (Student student in myClass) { Console.WriteLine(student); } 基本所 ...
- 常用的sql语句(找不同位数,找重复)
1.SQL找不同位数 select length(aae135),count(1) from ac01 group by length(aae135) ; ————————————————————— ...
- undefined function mysql_connect()解决方法
在配置apache+php+mysql后,打开一个php网页文件正常,但是php网页中连接数据库时,出现以下提示: Fatal error: Call to undefined function my ...
- C#调用Java类
C#调用Java类 (2011-01-07 14:02:05) 转载▼ 分类: Java学习 1. 在Eclipse中新建名称为hello的java project,此工程仅包含一个文件hell ...
- shell 和awk性能对比
time for ((i=0;i<10000;i++)) do ((sum+=i)); done real 0m0.086suser 0m0.079ssys 0m0.007s ...
- MongoDB入门一:安装与配置
引言 ——妈妈说名字太长排在后面或在标题堆儿中容易被大家发现. MongoDB的名字来源与单词humongous(极大的,巨大无比的)有关,它是一个可扩展.高性能.开源的NoSQL数据库. 之所以在g ...
- 用JavaScript修改浏览器tab标题
修改tab或者window的标题,是一项较老的实践.Gmail 用它来提示用户新的聊天消息,当有新的page通过AJAX加载的时候,本站同样用它更新tab title.这是怎样做到的呢?当时是通过设置 ...