WPF—数据双向绑定

作者:追风剑情 发布于:2019-8-23 18:29 分类:C#

MainWindow.xaml

<Window
        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:WpfTest"
        xmlns:Properties="clr-namespace:WpfTest.Properties" x:Class="WpfTest.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid Margin="0,0,0,0">
        <Label Content="X" HorizontalAlignment="Left" Margin="20,14,0,0" VerticalAlignment="Top"/>
        <Label Content="Y" HorizontalAlignment="Left" Margin="20,43,0,0" VerticalAlignment="Top"/>
        <Label Content="Z" HorizontalAlignment="Left" Margin="21,75,0,0" VerticalAlignment="Top"/>
        <!--重点是UpdateSourceTrigger=PropertyChanged, Mode=TwoWay-->
        <TextBox Name="textBox_X" Text="{Binding X, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="45,14,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <TextBox Name="textBox_Y" Text="{Binding Y, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="45,45,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <TextBox Name="textBox_Z" Text="{Binding Z, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="45,76,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" IsEnabled="False"/>
    </Grid>
</Window>

注意:如果要对控件的ItemsSource属性绑定数据源,那么数据源必须要是System.Collections.ObjectModel.ObservableCollection集合对象才能实现数据到控件的绑定(即,数据更改时UI自动刷新)。

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;//INotifyPropertyChanged
using System.Threading.Tasks;

namespace WpfTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Room room = new Room();

        public MainWindow()
        {
            InitializeComponent();
            //绑定数据源
            this.textBox_X.DataContext = room;
            this.textBox_Y.DataContext = room;
            this.textBox_Z.DataContext = room;
        }
    }

    public class Room : DependencyObject
    {
        public int X
        {
            get { return (int)GetValue(XProperty); }
            set {
                SetValue(XProperty, value);
                Console.WriteLine("set X={0}", value);
            }
        }

        public int Y
        {
            get { return (int)GetValue(YProperty); }
            set
            {
                SetValue(YProperty, value);
                Console.WriteLine("set Y={0}", value);
            }
        }

        public int Z
        {
            get { return (int)GetValue(ZProperty); }
            set
            {
                SetValue(ZProperty, value);
                Console.WriteLine("set Z={0}", value);
            }
        }

        // UI控件的值变化时回调
        private static void OnPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Room room = d as Room;
            switch (e.Property.Name)
            {
                case "X": room.X = (int)e.NewValue; break;
                case "Y": room.Y = (int)e.NewValue; break;
                case "Z": room.Z = (int)e.NewValue; break;
            }
        }

        public static readonly DependencyProperty XProperty =
        DependencyProperty.Register(
            "X",
            typeof(int),
            typeof(Room),
            new PropertyMetadata(0, OnPropertyChangedCallback, null)
        );

        public static readonly DependencyProperty YProperty =
        DependencyProperty.Register(
            "Y",
            typeof(int),
            typeof(Room),
            new PropertyMetadata(0, OnPropertyChangedCallback, null)
        );

        public static readonly DependencyProperty ZProperty =
        DependencyProperty.Register(
            "Z",
            typeof(int),
            typeof(Room),
            new PropertyMetadata(0, OnPropertyChangedCallback, null)
        );
    }
}

运行测试
1111.png2222.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号