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这个函数中,便于代码的修改和移植;

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值