WPF雙向數據綁定

WinForm雙向數據綁定:https://blog.csdn.net/weixin_42274148/article/details/104889310

參考:https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/data/binding-declarations-overview

綁定的四個要素:

  • 綁定目標對象。
  • 目標屬性。
  • 綁定源。
  • 要使用的綁定源中值的路徑。

msdn上給了很多綁定的示例,下邊僅給出一種綁定的代碼實現:

1、工程結構;

 2、xaml文件;

<Window x:Class="testBinding.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:testBinding"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Loaded="Window_Loaded">
    <Grid>
        <Button x:Name="button" Content="Add" HorizontalAlignment="Left" Margin="340,109,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
        <TextBox x:Name="textBox" Text="{Binding Sum}" HorizontalAlignment="Left" Height="23" Margin="149,109,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>

3、 Data.cs;

using System;
using System.Collections.Generic;
using System.ComponentModel; //添加命名空間
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testBinding
{
    class Data: INotifyPropertyChanged //.net定義的接口
    {
        private int sum;
        
        public int Sum
        {
            get 
            {
                return this.sum;
            }
            set
            {
                this.sum = value;
                if (PropertyChanged != null) 
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Sum")); //觸發事件
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged; //實現接口
    }
}

4、後臺代碼;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

namespace testBinding
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        private Data myData = new Data(); 
        //項目中可以把需要交互的數據放在一個單獨的類中,並將這個類與前端控件綁定,
        //通常,需要在該類中實現INotifyPropertyChanged接口,纔可以實現雙向的綁定
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            myData.Sum = 0;
            textBox.DataContext = myData; //綁定數據上下文
        }

        private void button_Click(object sender, RoutedEventArgs e)  //測試代碼
        {
            myData.Sum++;
        }
    }
}

5、結尾

綁定的目的是實現前後端代碼的分離,如果不使用綁定,還可以通過直接給控件賦值,或者通過控件讀取值來實現。這樣會導致前後端代碼高度耦合,修改極度繁瑣;而通過以上方式,可以使前後端代碼的耦合被限定在Window_Loaded這個函數中,便於代碼的修改和移植;

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章