World War I Windows 10 (20) and World War I windows

Source: Internet
Author: User

World War I Windows 10 (20) and World War I windows

[Download source code]


Backwater world war I Windows 10 (20)-binding: DataContextChanged, UpdateSourceTrigger, custom conversion of bound data



Author: webabcd


Introduction
Bind to Windows 10

  • Events triggered when the DataContext of DataContextChanged-FrameworkElement changes
  • UpdateSourceTrigger-data update trigger Method
  • Perform custom conversion on bound data



Example
1. Demonstrate DataContextChanged usage
Bind/DataContextChanged. xaml

<Page x: Class = "Windows10.Bind. dataContextChanged "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: local =" using: Windows10.Bind "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 = "10 0 10"> <TextBlock Name = "lblMsg" Margin = "5"/> <button x: name = "btnChange" Content = "change the data context of listBox" Click = "btnChange_Click" Margin = "5"/> <ListBox x: name = "listBox" ItemsSource = "{Binding}" DataContextChanged = "listBox_DataContextChanged" Background = "Orange" Margin = "5"/> </StackPanel> </Grid> </Page>

Bind/DataContextChanged. xaml. cs

/** Events triggered when the DataContext of DataContextChanged-FrameworkElement changes */using System; using System. collections. generic; using Windows. UI. xaml; using Windows. UI. xaml. controls; namespace Windows10.Bind {public sealed partial class DataContextChanged: Page {public DataContextChanged () {this. initializeComponent (); this. loaded + = DataContextChanged_Loaded;} private 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-the event triggered when the data context changes * // lblMsg after the data context changes. text = "data context changed:" + DateTime. now. toString ("hh: mm: ss ");}}}


2. Demonstrate UpdateSourceTrigger usage
Bind/UpdateSourceTrigger. xaml

<Page x: Class = "Windows10.Bind. UpdateSourceTrigger" xmlns =" http://schemas.microsoft.com/winfx/2006/xaml /Presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "Xmlns: local =" using: Windows10.Bind "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 = "10 0 10"> <TextBlock Name = "lblMsg" Foreground = "Orange" Margin =" 5 "/> <! -- UpdateSourceTrigger-data update trigger method Default-triggers PropertyChanged when the focus is lost-triggers Explicit when the attribute value changes-BindingExpression is required. updateSource () display trigger --> <TextBox Text = "{Binding Text, Mode = TwoWay, ElementName = lblMsg, updateSourceTrigger = Default} "Margin =" 5 "/> <TextBox Text =" {Binding Text, Mode = TwoWay, ElementName = lblMsg, updateSourceTrigger = PropertyChanged} "Margin =" 5 "/> <TextBox Name =" txtExplicit "Text =" {Binding Text, Mode = TwoWay, ElementName = lblMsg, updateSourceTrigger = Explicit} "Margin =" 5 "/> <Button Name =" btnBinding "Content =" display trigger Update "Click =" btnBinding_Click "Margin =" 5 "/> </ stackPanel> </Grid> </Page>

Bind/UpdateSourceTrigger. xaml. cs

/** UpdateSourceTrigger-trigger Method of Data Update * Default-trigger * PropertyChanged when the focus is lost-trigger * Explicit after the attribute value is changed-BindingExpression is required. updateSource () display trigger */using Windows. UI. xaml; using Windows. UI. xaml. controls; using Windows. UI. xaml. data; namespace Windows10.Bind {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 ();}}}


3. demonstrate how to perform custom conversion on the bound data
Bind/BindingConverter. xaml

<Page x: Class = "Windows10.Bind. BindingConverter" xmlns =" http://schemas.microsoft.com/winfx/2006/xaml /Presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "Xmlns: local =" using: Windows10.Bind "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 =" 10 0 10 10 "> <! -- Configure IValueConverter resources --> <StackPanel. resources> <local: IntegerLetterConverter x: Key = "IntegerLetterConverter"/> </StackPanel. resources> <Slider Name = "slider1" Minimum = "97" Maximum = "122" Value = "1" Width = "300" Background = "White" HorizontalAlignment = "Left" Margin = "5"/> <! -- Demonstrate how to use the Converter, ConverterParameter, and ConverterLanguage of Binding. Note: ConverterParameter and ConverterLanguage do not support Binding, only one static value can be configured (but some features that cannot be implemented in xaml can be implemented in the C # end. For details, refer to the example below) --> <TextBox Name = "textBox1" Width = "300" HorizontalAlignment = "Left" Margin = "5" Text = "{Binding ElementName = slider1, Path = Value, mode = TwoWay, Converter = {StaticResource IntegerLetterConverter}, ConverterParameter = param, ConverterLanguage = Zh} "/> <Slider Name =" slider2 "Minimum =" 97 "Maximum =" 122 "Value =" 1 "Width =" 300 "Background =" White "HorizontalAlignment = "Left" Margin = "5"/> <! -- Use the Converter, ConverterParameter, and, converterLanguage --> <TextBox Name = "textBox2" Width = "300" HorizontalAlignment = "Left" Margin = "5"/> <TextBlock Name = "lblMsg" TextWrapping = "Wrap "/> </StackPanel> </Grid> </Page>

Bind/BindingConverter. xaml. cs

/** Demonstrate how to customize the conversion of bound data */using System; using Windows. UI. xaml; using Windows. UI. xaml. controls; using Windows. UI. xaml. data; namespace Windows10.Bind {public sealed partial class BindingConverter: Page {public BindingConverter () {this. initializeComponent (); this. loaded + = BindingConverter_Loaded;} private void BindingConverter_Loaded (object sender, RoutedEventArgs e) {// instantiate the Binding object Binding binding = New Binding () {ElementName = nameof (slider2), Path = new PropertyPath (nameof (Slider. value), Mode = BindingMode. twoWay, // The default value is OneWay's Converter = new IntegerLetterConverter (), ConverterParameter = lblMsg, // set ConverterParameter to a specified control, which cannot be implemented in xaml, however, ConverterLanguage = "zh"} can be implemented in C #; // associate the target object's target attribute with the specified Binding object BindingOperations. setBinding (textBox2, TextBox. textProperty, bindin G) ;}} // customize a class that implements the IValueConverter interface, which is used to customize the conversion of bound data to public sealed class IntegerLetterConverter: IValueConverter {// <summary> // forward converter. When you pass the value from the data source to the bound destination, the Data Binding engine calls this method /// </summary> /// <param name = "value"> value before conversion </param> /// <param name = "targetType"> converted data type </param> // The parameter used by the <param name = "parameter"> converter (it is passed through the ConverterParameter of the Binding) </param> // <param name = "language"> region information used by the converter (it is transmitted through the ConverterLanguage of Binding) </param> // <returns> converted value </returns> public object Convert (object value, Type targetType, object parameter, String language) {if (parameter! = Null & parameter. getType () = typeof (TextBlock) {(TextBlock) parameter ). text = $ "value: {value}, targetType: {targetType}, parameter: {parameter}, language: {language}" ;}int v = (int) (double) value; return (char) v ;}/// <summary> /// reverse converter. When you pass a value from the bound destination to the data source, the Data Binding engine calls this method /// </summary> /// <param name = "value"> value before conversion </param> /// <param name = "targetType"> converted data type </param> // The parameter used by the <param name = "parameter"> converter (it is passed through the ConverterParameter of the Binding) </param> // <param name = "language"> region information used by the converter (it is transmitted through the ConverterLanguage of Binding) </param> // <returns> converted value </returns> public object ConvertBack (object value, Type targetType, object parame Ter, string language) {if (parameter! = Null & parameter. getType () = typeof (TextBlock) {(TextBlock) parameter ). text = $ "value: {value}, targetType: {targetType}, parameter: {parameter}, language: {language}" ;}int v = (string) value ). toCharArray () [0]; return v ;}}}



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.