《深入浅出WPF》读书笔记.6binding系统(上)
背景
这一章是核心内容。databinding可以理解为实现逻辑层和展示层的桥梁。
binding基础
如何理解binding
<Window x:Class="BindingSysDemo.BindingSysDemo1"
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:BindingSysDemo"
mc:Ignorable="d"
Title="BindingSysDemo1" Height="300" Width="400">
<Grid>
<Canvas>
<TextBox Width="120" Canvas.Left="129" Canvas.Top="108" x:Name="tb1"></TextBox>
<Button Content="点击一下" Width="120" Canvas.Left="129" Canvas.Top="142" Click="Button_Click"></Button>
</Canvas>
</Grid>
</Window>
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.Shapes;
namespace BindingSysDemo
{
/// <summary>
/// BindingSysDemo1.xaml 的交互逻辑
/// </summary>
public partial class BindingSysDemo1 : Window
{
Student student = null;
public BindingSysDemo1()
{
InitializeComponent();
//student = new Student();
//Binding binding = new Binding();
//binding.Source = student;
//binding.Path = new PropertyPath("Name");
//BindingOperations.SetBinding(this.tb1, TextBox.TextProperty, binding);
this.tb1.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = student = new Student() });
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.student.Name += "Name";
}
}
}
BindingOperations.SetBinding(this.tb1, TextBox.TextProperty, binding);
git代码👇
GitHub - wanghuayu-hub2021/WpfBookDemo: 深入浅出WPF的demo
代码直接先给了,剩下的部分会之后进行总结,这本书所有的demo在学习完成后都会提交到上述git地址