重新想象 Windows 8.1 Store Apps (82) - 绑定: DataContextChanged, TargetNullValue, FallbackValue, UpdateSourceTrigger

原文:重新想象 Windows 8.1 Store Apps (82) - 绑定: DataContextChanged, TargetNullValue, FallbackValue, UpdateSourceTrigger

[源码下载]

重新想象 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(0, 1000).ToString().PadLeft(3, '0') };
        }

        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
[源码下载]

时间: 2024-09-04 09:23:48

重新想象 Windows 8.1 Store Apps (82) - 绑定: DataContextChanged, TargetNullValue, FallbackValue, UpdateSourceTrigger的相关文章

重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件

原文:重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件 [源码下载] 重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之通信的新特性 下载数据(显示下载进度,将下载数据保存到本地) 上传数据(显示上传进度) 上传文件 示例HTTP 服务端WebServer/HttpDemo.

重新想象 Windows 8.1 Store Apps (80) - 控件增强: WebView 之基本应用, POST 数据, 与 JavaScript 交互

原文:重新想象 Windows 8.1 Store Apps (80) - 控件增强: WebView 之基本应用, POST 数据, 与 JavaScript 交互 [源码下载] 重新想象 Windows 8.1 Store Apps (80) - 控件增强: WebView 之基本应用, POST 数据, 与 JavaScript 交互 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之控件增强 WebView 的基本应用 WebView 通过 POST 请

重新想象 Windows 8.1 Store Apps (79) - 控件增强: MediaElement, Frame

原文:重新想象 Windows 8.1 Store Apps (79) - 控件增强: MediaElement, Frame [源码下载] 重新想象 Windows 8.1 Store Apps (79) - 控件增强: MediaElement, Frame 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之控件增强 MediaElement - 播放视频或音频的控件 Frame - 框架控件,用于导航内容 示例1.演示 MediaElement 的新特性M

重新想象 Windows 8.1 Store Apps (85) - 警报通知(闹钟), Tile 的新特性

原文:重新想象 Windows 8.1 Store Apps (85) - 警报通知(闹钟), Tile 的新特性 [源码下载] 重新想象 Windows 8.1 Store Apps (85) - 警报通知(闹钟), Tile 的新特性 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之通知的新特性 警报通知(闹钟) Tile 的新特性 示例1.演示 win8.1 中新增的警报 toast(闹钟)AlarmToast.xaml <Page x:Class=&quo

重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML

原文:重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML [源码下载] 重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之 TTS(Text To Speech) Speak Text Speak SSML 示例1.演示如何通过 TTS 朗读一段文本,以及如何将其保

重新想象 Windows 8.1 Store Apps (92) - 其他新特性: CoreDispatcher, 日历, 自定义锁屏系列图片

原文:重新想象 Windows 8.1 Store Apps (92) - 其他新特性: CoreDispatcher, 日历, 自定义锁屏系列图片 [源码下载] 重新想象 Windows 8.1 Store Apps (92) - 其他新特性: CoreDispatcher, 日历, 自定义锁屏系列图片 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之其他新特性 CoreDispatcher 的新特性 "日历"的相关操作 自定义锁屏时需要显示的系列

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

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

重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性

原文:重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性 [源码下载] 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之后台任务的新特性 下载和上传的新特性 程序启动前预下载网络资源 后台任务

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

原文:重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup [源码下载] 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之控件增强 ScrollViewer - 滚动视图控件的增强 FlipView - 滑动视图控件的增强 Popup