World War I Windows 10 (18) and World War I 18

Source: Internet
Author: User

World War I Windows 10 (18) and World War I 18

[Download source code]

Backwater world war I Windows 10 (18)-binding: bind with Element, and Indexer, TargetNullValue, FallbackValue



Author: webabcd


Introduction
Bind to Windows 10

  • Bind to Element
  • Bind with Indexer
  • TargetNullValue-value displayed when the bound data is null
  • FallbackValue-value displayed when binding fails



Example
1. demonstrate how to bind an Element
Bind/BindingElement. xaml

<Page
    x: Class = "Windows10.Bind.BindingElement"
    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">

            <!-
                This example is used to demonstrate how to bind with Element and the difference between OneTime, OneWay, TwoWay
            ->

            <!-
                OneTime binding element
            ->
            <TextBox Text = "{Binding ElementName = txtOneTime, Path = Text, Mode = OneTime}" />
            <TextBox Name = "txtOneTime" Text = "OneTime" Margin = "0 10 0 0" />
            

            <!-
                OneWay way to bind elements (OneWay is the default way)
            ->
            <TextBox Text = "{Binding ElementName = txtOneWay, Path = Text, Mode = OneWay}" Margin = "0 30 0 0" />
            <TextBox Name = "txtOneWay" Text = "OneWay" Margin = "0 10 0 0" />

            <!-
                TwoWay way to bind elements (simultaneously demonstrate another way of writing Binding tag, is to directly put the path specified by Path behind Binding)
            ->
            <TextBox Text = "{Binding Text, ElementName = txtTwoWay, Mode = TwoWay}" Margin = "0 30 0 0" />
            <TextBox Name = "txtTwoWay" Text = "TwoWay" Margin = "0 10 0 0" />

            <!-
                TwoWay way to bind elements (specify the Binding object on the C # side)
            ->
            <TextBox Name = "textBox1" Margin = "0 30 0 0" />
            <TextBox Name = "textBox2" Text = "TwoWay" Margin = "0 10 0 0" />

        </ StackPanel>
    </ Grid>
</ Page>
Bind / BindingElement.xaml.cs

/ *
 * Demonstrate how to bind with Element
 * /

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace Windows10.Bind
{
    public sealed partial class BindingElement: Page
    {
        public BindingElement ()
        {
            this.InitializeComponent ();

            this.Loaded + = BindingElement_Loaded;
        }

        // Do the binding on the C # side
        private void BindingElement_Loaded (object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            // Instantiate the Binding object
            Binding binding = new Binding ()
            {
                ElementName = nameof (textBox2),
                Path = new PropertyPath (nameof (TextBox.Text)),
                Mode = BindingMode.TwoWay // The default is OneWay
            };

            // Associate the target property of the target object with the specified Binding object
            BindingOperations.SetBinding (textBox1, TextBox.TextProperty, binding);
        }
    }
}

2. Demonstrate how to bind with Indexer
Bind / BindingIndexer.xaml

<Page
    x: Class = "Windows10.Bind.BindingIndexer"
    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">

            <!-Demonstrate how to bind an element in the collection->
            <TextBlock Name = "textBlock" Text = "{Binding Path = [3]}" />

            <!-Demonstrate how to bind a certain property of an object in the collection->
            <TextBlock Name = "textBlock2" Text = "{Binding Path = [5] .Name}" Margin = "0 10 0 0" />

            <!-Demonstrate how to bind an indexer of type string->
            <TextBlock Name = "textBlock3" Text = "{Binding Path = [webabcd]}" Margin = "0 10 0 0" />

            <!-Demonstrate how to bind the data of the specified key in the dictionary table->
            <TextBlock Name = "textBlock4" Text = "{Binding Path = [hello]}" Margin = "0 10 0 0" />

            <!-Demonstrate how to bind the indexer on the C # side->
            <TextBox Name = "textBox" Margin = "0 10 0 0" />

        </ StackPanel>
    </ Grid>
</ Page>
Bind / BindingIndexer.xaml.cs

/ *
 * Demonstrate how to bind with Indexer
 * /

using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows10.Common;

namespace Windows10.Bind
{
    public sealed partial class BindingIndexer: Page
    {
        public BindingIndexer ()
        {
            this.InitializeComponent ();

            this.Loaded + = BindingIndexer_Loaded;

            BindingDemo ();
        }

        private void BindingIndexer_Loaded (object sender, RoutedEventArgs e)
        {
            // Used to demonstrate how to bind an element in the collection
            List <string> list = new List <string> ();
            for (int i = 0; i <10; i ++)
            {
                list.Add ("Index:" + i.ToString ());
            }
            textBlock.DataContext = list;

            // Used to demonstrate how to bind a property of an object in the collection
            textBlock2.DataContext = TestData.GetEmployees ();

            // Used to demonstrate how to bind an indexer of type string
            textBlock3.DataContext = this;

            // Used to demonstrate how to bind the data of the specified key in the dictionary table
            Dictionary <string, string> dic = new Dictionary <string, string> () {{"hello", "hello webabcd"}};
            textBlock4.DataContext = dic;
        }

        // Customize an indexer
        public object this [string indexer]
        {
            get
            {
                return "string:" + indexer;
            }
        }



        // Bind the indexer on the C # side
        private void BindingDemo ()
        {
            textBox.DataContext = this;

            // Instantiate the Binding object
            Binding binding = new Binding ()
            {
               Path = new PropertyPath ("[wanglei]")
            };
            
            // Associate the target property of the target object with the specified Binding object
            BindingOperations.SetBinding (textBox, TextBox.TextProperty, binding);

            / *
             * Note: It is normal to do the above binding on TextBox after testing. But if you do the above binding in TextBlock, the runtime will report an error. Attempted to read or write protected memory. This is often an indication that other memory is corrupt. I do n’t know why
             * /
        }
    }
}

3. Demonstrate the use of TargetNullValue and FallbackValue in Binding
Bind / TargetNullValueFallbackValue.xaml

<Page
    x: Class = "Windows10.Bind.TargetNullValueFallbackValue"
    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">

            <!-
                FallbackValue-the value displayed when the binding fails
            ->
            <TextBlock Name = "textBlock1" Text = "{Binding Path = MyName, FallbackValue = 'Default value when binding fails'}" Margin = "5" />

            <!-
                TargetNullValue-the value displayed when the bound data is null
            ->
            <TextBlock Name = "textBlock2" Text = "{Binding Path = MyName, TargetNullValue = 'The return value of the binding data is null'}" Margin = "5" />

        </ StackPanel>
    </ Grid>
</ Page>
Bind / TargetNullValueFallbackValue.xaml.cs

/ *
 * Demonstrate the usage of TargetNullValue and FallbackValue in Binding
 * /

using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace Windows10.Bind
{
    public sealed partial class TargetNullValueFallbackValue: Page
    {
        public TargetNullValueFallbackValue ()
        {
            this.InitializeComponent ();
        }

        protected override void OnNavigatedTo (NavigationEventArgs e)
        {
            // Provide data context for textBlock2
            textBlock2.DataContext = this;

            / *
            // Instantiate the Binding object
            Binding binding = new Binding ()
            {
                Path = new PropertyPath ("Name"),
                TargetNullValue = "TargetNullValue",
                FallbackValue = "FallbackValue"
            };

            // Associate the target property of the target object with the specified Binding object
            BindingOperations.SetBinding (textBlock2, TextBox.TextProperty, binding);
            * /
        }

        public string MyName {get; set;} = null;
    }
}


OK

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.