posts - 615, comments - 10492, trackbacks - 594, articles - 0
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[源码下载]


重新想象 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
[源码下载]