[源码下载]

重新想象 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. console 让 js 调试更简单

    浏览器的控制台(console)是最重要的面板,主要作用是显示网页加载过程中产生各类信息. 显示信息 console.log('hello world'); console.debug('debug' ...

  2. 【WebMisCentral WMC】基于Extjs 4.2x的企业级用户授权认证中心系统(SSO+AM+SM),多租户SAAS应用

    http://saas.chinacloudtech.com 题记 三年磨一剑,在企业信息化的道路上已经走了3年之久了,3年多时间里做了很多,突破了很多:有无奈和辛酸,也有收货与喜悦:自我价值也在不断 ...

  3. H5页面设计器,仿有赞商城页面在线设计器,比富文本框更友好的内容编辑器

    基本上每个web应用,都会牵扯到内容编辑,尤其是移动的web应用,微信开发之类的.页面内容自定义是最常用的功能了,之前大部分解决方案都是采用富文本框编辑器kindeditor,ueditor,cked ...

  4. TortoiseSVN and TortoiseGit 版本控制图标不见了

    突然有一天,代码文件夹上的版本控制图标不见了. 注册表中,将文件夹名重命名,让版本控制的靠前,Computer \ HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft ...

  5. 百度地图API 关键字模糊搜索

    http://api.map.baidu.com/place/v2/search?q=广场&region=汕头&output=json&ak=5E56A48675a5cd09a ...

  6. 纯CSS3实现3D特效的iPhone 6动画

    iPhone 6发布不久,屌丝怎能买得起,不过作为程序员,今天看到一个用纯CSS3绘制的iPhone 6,由于CSS3特性的运用,带有点3D的动画特效,大家可以先来看看在线演示效果. 在线演示    ...

  7. CentOS 7 Vmware虚拟机 /root空间不足解决方法(使用gparted live)

    1,关闭虚拟机,编辑虚拟机设置,增加虚拟磁盘的大小,我这里增加10GB 2,连接CDrom到ISO文件(gparted-live-0.19.0-1-i486.iso),使用gparted live启动 ...

  8. Object c 基础知识

    文件类型说明:.h 头文件,用于定义类.实例变量及类中的方法等定义信息(interface)..m 源文件,定义方法体,可实现objce-c和c方法(implementation)..mm c++源文 ...

  9. Microsoft 2013 新技术学习笔记 三

    什么是代码结构的组织?asp.net MVC 5 默认创建出的几个目录的标准含义分别如下: Controllers目录存放MVC模式中的Controler Models目录存放MVC模式中的Model ...

  10. iOS开发- Xcode 7添加pch文件

    1.打开你的Xcode工程. 在Supporting Files目录下,选择 File > New > File > iOS > Other > PCH File 然后点 ...