[源码下载]

重新想象 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的更多相关文章

  1. 重新想象 Windows 8.1 Store Apps 系列文章索引

    [源码下载] [重新想象 Windows 8 Store Apps 系列文章] 重新想象 Windows 8.1 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  2. 重新想象 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 ...

  3. 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar

    [源码下载] 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...

  4. 重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker

    [源码下载] 重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker 作者:webabcd 介绍重新想象 Windows 8.1 ...

  5. 重新想象 Windows 8.1 Store Apps (74) - 新增控件: Flyout, MenuFlyout, SettingsFlyout

    [源码下载] 重新想象 Windows 8.1 Store Apps (74) - 新增控件: Flyout, MenuFlyout, SettingsFlyout 作者:webabcd 介绍重新想象 ...

  6. 重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink

    [源码下载] 重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink 作者:webabcd 介绍重新想象 Windows 8.1 Store A ...

  7. 重新想象 Windows 8.1 Store Apps (76) - 新增控件: SearchBox

    [源码下载] 重新想象 Windows 8.1 Store Apps (76) - 新增控件: SearchBox 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之 ...

  8. 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性

    [源码下载] 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件 ...

  9. 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup

    [源码下载] 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup 作者:webabcd 介绍重新想象 Wind ...

随机推荐

  1. 4kbps~15kbps语音编码器基础框架ACELP

  2. ASP.NET MVC应用require.js实践

    这里有更好的阅读体验和及时的更新:http://pchou.info/javascript/asp.net/2013/11/10/527f6ec41d6ad.html Require.js是一个支持j ...

  3. 学习之路三十六:SQL知识总结 - [游标||字符串分割]

    好久没有写文章了,今天把前不久项目用到的SQL知识总结一下. 一丶字符串分割 SQL内置函数中是没有Split分割函数的,所以需要自己去实现,不多说,上代码: )) RETURNS @result T ...

  4. openwrt 编译newifi 应用程序

    首先找交叉编译工具( toolchain ) Development Snapshots http://downloads.openwrt.org/snapshots/trunk/ 我需要的版本是 m ...

  5. 在Windows2008系统中利用IIS建立FTP服务器

    一.服务器管理器   1.2008的系统使用服务器管理器,选择角色,因为我之前已经开启了IIS服务器角色,所以我现在只要添加角色服务即可,如果你没有开启过的话,直接添加角色即可.   2.选择WEB服 ...

  6. ubuntu下 mysql5.6.4 +sphinx安装

    安装mysql 5.6.4 下载源码 安装cmake sudo apt-get install cmake 进入mysql源码包: 创建mysql用户与用户组 groupadd mysql usera ...

  7. ODAC (V9.5.15) 学习笔记(二十)大数据量获取处理

    ODAC获取数据的效率比较高,在Web程序中希望能够更快获取第一页的数据时,可以有几种方式: 1.在数据库中进行分页处理: 2.获取所有数据,只是快速返回第一页数据. 第一种方案对应用服务器资源消耗最 ...

  8. php网页切图/js切图

    PhantomJS抓取网站页面信息以及网站截图 http://phantomjs.org/download.html PHP imagegrabscreen和imagegrabwindow(截取网站缩 ...

  9. Linux发行版大全

    基于Debian  Adamantix:基于Debian,特别关注安全.  Amber Linux:基于Debian,针对拉脱维亚用户作了一些定制.  ASLinux Desktop:西班牙语,基于D ...

  10. Using the Cordova Camera API

    使用ionic开发一款android或ios应用,估计少不了使用到Camera API,这里记录下使用过程. 创建空的ionic应用 ionic start myTabs tabs 通过cd demo ...