1.概要
1.1 说明
使用代码的方式绑定。
1.2 要点
var bindo = new Binding("Text");bindo.Source = textso;//绑定源text1.SetBinding(TextBox.TextProperty, bindo);//绑定数据源和对象
2.代码
<Window x:Class="WpfApp4.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:WpfApp4"mc:Ignorable="d"Title="MainWindow" Height="200" Width="300"><WrapPanel><TextBox Name="textso" Width="200"></TextBox><TextBox Name="text1" Width="200"></TextBox></WrapPanel>
</Window>
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;namespace WpfApp4
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();bind();}public void bind(){var bindo = new Binding("Text");bindo.Source = textso;text1.SetBinding(TextBox.TextProperty, bindo);}}
}
3.运行结果