[源码下载]

重新想象 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. prim算法java版

    public class Prim { static int MAX = 65535; public static void prim(int[][] graph, int n){ char[] c ...

  2. android自定义RadioGroup实现可以添加多种布局

    android自带的RadioGroup是继承自LinearLayout,如果布局的时候不是直接写radiobutton,即radiobutton外面还包了一层容器,这时分组是不成功的,因为查找不到r ...

  3. WinFrom 登录窗体 密码保存效果

    WinFrom 登录窗体 保存密码效果 开发CS程序的程序员都会遇到 今天突然想把这个功能加到我的项目中 之后总结下 不多说 上图   如果关闭程序 下次在登录的时候 用户名.密码会自动保留下来  一 ...

  4. RadioButton 的background属性表现特征

    对于radiaoButton,应该很多人都用过.下面看一个场景     上方时radiogroup,细致观察发现左1,文字开始位置和右1文字开始位置不同,这是为何呢? 查看布局: <RadioB ...

  5. apache2.4 以上设定gzip压缩

    国内很多文章都是抄来抄去,用不成,用我这个就可以了 编辑 http.conf 文件 去掉 #LoadModule headers_module modules/mod_headers.so 前面的注释 ...

  6. thinkphp 3.2.3+Admin LTE后台框架

  7. java基本加密算法

    简单的java加密算法有: BASE64 严格地说,属于编码格式,而非加密算法 MD5(Message Digest algorithm 5,信息摘要算法) SHA(Secure Hash Algor ...

  8. Spring.Net.FrameworkV3.0 版本发布了,感谢大家的支持

    Spring.Net.FrameworkV3.0 版本发布了,感谢大家的支持. Spring.Net.Framework,基于.NET的快速信息化系统开发.整合框架,为企业或个人在.NET环境下快速开 ...

  9. R包介绍

    R语言的使用,很大程度上是借助各种各样的R包的辅助,从某种程度上讲,R包就是针对于R的插件,不同的插件满足不同的需求,截至2013年3月6日,CRAN已经收录了各类包4338个. 一. R语言包的安装 ...

  10. 模拟Jquery选择器

    目前实现的功能有以下几点: 1.$("#adom"); // 返回id为adom的DOM对象 2.$("a"); // 返回一个a标签的数组 3.$(" ...