Imagine Windows 8.1 Store Apps (82)

Source: Internet
Author: User

[Download source code]


Re-imagine Windows 8.1 Store Apps (82)-binding: DataContextChanged, TargetNullValue, FallbackValue, UpdateSourceTrigger



Author: webabcd


Introduction
Re-imagine binding Windows 8.1 Store Apps

  • Events triggered when the DataContext of DataContextChanged-FrameworkElement changes
  • TargetNullValue-value to be displayed when the bound data is null
  • FallbackValue-the value to be displayed when the binding fails (the return value cannot be returned)
  • UpdateSourceTrigger-data update trigger method on the UI



Example
1. Demonstrate the DataContextChanged Application
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 = "change data context" 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

/** Events triggered when the DataContext of DataContextChanged-FrameworkElement changes *** for details about the binding basics, see :* 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) {// specify the data context listBox. dataContext = new List <string> {"a", "B", "c" };} private void btnChange_Click (object sender, RoutedEventArgs e) {// modify the data context 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-events triggered after the data context changes * // lblMsg after the data context changes. text = "Data Source changed:" + DateTime. now. toString ("hh: mm: ss ");}}}


2. demonstrate the application of TargetNullValue and 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-value to be displayed when Binding fails (cannot return value) --> <TextBlock FontSize = "14.667" Text = "{Binding Path = xxx, fallbackValue = 'default value when binding fails '} "/> <! -- TargetNullValue-the value to be displayed when the bound data is null --> <TextBlock FontSize = "14.667" Text = "{Binding Path = Name, targetNullValue = 'Return value of binding null'} "Margin =" 0 10 0 0 "/> </StackPanel> </Grid> </Page>

TargetNullValueFallbackValue. xaml. cs

/** TargetNullValue-the value to be displayed when the bound data is null * FallbackValue-when the binding fails (the return value cannot be returned) the value to be displayed when *** about binding basics see * 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. demonstrate the application of 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-Method of triggering data update on UI Default-triggering PropertyChanged when the focus is lost-triggering Explicit when the attribute value changes-BindingExpression is required. updateSource () display trigger --> <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 =" display trigger Update "Click =" btnBinding_Click "Margin =" 0 10 0 0 "/> </StackPanel> </Grid> </Page>

UpdateSourceTrigger. xaml. cs

/** Trigger method of UpdateSourceTrigger-data update on UI * Default-trigger * PropertyChanged-trigger * Explicit after the property value changes-BindingExpression is required. updateSource () displays trigger *** for the basics of binding, see * 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) {// display the data update BindingExpression that triggers txtExplicit. be = txtExplicit. getBindingExpression (TextBox. textProperty); be. updateSource ();}}}



OK
[Download source code]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.