In layman's language WPF (Binding Part 1)

Binding in the industry has been a transliteration from, called "Binding". Binding source is a logical data object , the target is the UI layer above the control object . Binding data delivered by the UI layer, is shown out of the UI layer, it will complete the process of the data driver UI.

By following a very simple Liezi come into our most primitive Binding:

<Window x:Class="BindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BindingTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="100" Width="400">
    <StackPanel>
        <TextBox Name="MyTextBlock" BorderBrush="Black" Margin="3"/>
        <Button Content="Add Age" Margin="3" Click="Button_Click"/>
    </StackPanel>
</Window>

Binding An automatic mechanism is that when property values ​​bound to change the background, automatically notifies the UI elements, how to get the properties have the ability to do, in fact, only need to trigger the PropertyChanged event attribute set time. We do not need to declare this event, we just need to implement INotifyPropertyChanged interface. Implements this interface student classes are as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BindingTest.Models
{
    public class Student : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public int _age;
        public int Age
        {
            get { return this._age; }
            set
            {
                if (value != 0)
                {
                    this._age = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Age)));
                }
            }
        }
    }
}

The final form of the code behind:

namespace BindingTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Student studen;
        public MainWindow()
        {
            InitializeComponent();
            studen = new Student();
            Binding binding = new Binding();
            binding.Source = studen;
            binding.Path = new PropertyPath("Age"); 
            BindingOperations.SetBinding ( the this .MyTextBlock, TextBox.TextProperty, Binding); // Set the binding properties of the source and target objects 
        } 

        Private  void the Button_Click ( Object SENDER, the RoutedEventArgs E) 
        { 
            studen.Age + = . 1 ; // change an object, the page will change the textbox data 
        } 
    } 
}

 1 binding source path and

  1.1 Control as a binding source

    Sometimes the need to make some correlation effects between Ui elements may be utilized Binding association between the controls, the following code is the text of the TextBlock and Slider (slider) of value were correlated. Run the following code will find that when Slider slide, the value of the TextBlock will change.

<Window x:Class="BindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BindingTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="100" Width="400">
    <StackPanel>
        <Slider Value="0" Name="slider1" Minimum="0" Maximum="100"></Slider>
        <TextBlock Text="{Binding ElementName=slider1,Path=Value}" Margin="3"></TextBlock>
    </StackPanel>
</Window>

It can be set by setting the Binding of Mode data flow, generally TwoWay, OneWay, OnTime, OneWayToSource and Default, if not set is Default, if your goal control is user editable is bidirectional, such as TextBox if it is not editable , then that is one-way, such as TextBlock.

 

 

 

Guess you like

Origin www.cnblogs.com/qwqwQAQ/p/11468958.html