深入浅出WPF(Binding篇1)

Binding在业界的使用一直是音译而来的,称为"Binding"。Binding的源是逻辑数据对象,目标则是UI层上面的控件对象。数据通过Binding送达UI层,被UI层展示出来,也就完成了数据驱动UI的过程了。

下面通过一个很简单的列子来引入我们最原始的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有一种自动机制,就是当后台绑定的属性值发生改变时,会自动通知给UI元素,怎么样才能让属性具备这样的能力呢,其实只需要在属性set的时候去触发PropertyChanged事件。这个事件不需要我们声明,只需要我们去实现INotifyPropertyChanged接口。实现了此接口的学生类如下:

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)));
                }
            }
        }
    }
}

最终在窗体的后台代码中:

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(this.MyTextBlock, TextBox.TextProperty, binding);//设置目标对象的属性和源的绑定
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            studen.Age += 1;//改变对象,页面中textbox中数据也会变化
        }
    }
}

 1绑定的源和路径

  1.1 控件作为binding的源

    Ui元素之间有时候需要进行一些关联效果可以利用Binding在控件之间建立关联,下面的代码就是将TextBlock的text和Slider(滑动条)的value进行了关联。运行下面这段代码就会发现当Slider滑动的时候,TextBlock中的值会随之改变。

<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>

可以通过设置Binding的Mode来设置数据流向,一般有TwoWay,OneWay,OnTime,OneWayToSource以及Default,如果不设置就是Default,如果你的目标控件是用户可编辑的就是双向的,比如TextBox如果是不可编辑的,那么就是单向的,比如TextBlock。

 

猜你喜欢

转载自www.cnblogs.com/qwqwQAQ/p/11468958.html