wpf 数据绑定 联系

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;

变量:
namespace 联系 { public class DA:INotifyPropertyChanged { private string _Name; public string Name { get { return _Name; } set { _Name = value; OnPropertyChanged("Name"); } } private string _Pwd;
public string Pwd { get { return _Pwd; } set { _Pwd = value; OnPropertyChanged("Pwd"); } } public event PropertyChangedEventHandler PropertyChanged;// public virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } }
表里添加:
<Window x:Class="联系.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid> <Label Content="显示" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="19,26,0,0"/> <TextBox Name="aaa" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Top" Width="120" Margin="103,29,0,0"/> <TextBox HorizontalAlignment="Left" Height="23" Margin="103,97,0,0" Text="{Binding Text, ElementName=aaa}" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <Button Content="确定" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="103,150,0,0" Click="Button_Click_1"/> </Grid> </Window>
方法:
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.Shapes; namespace 联系 { /// <summary> /// Window1.xaml 的交互逻辑 /// </summary> public partial class Window1 : Window { DA da; public Window1() { InitializeComponent(); da = new DA() { Name="admin",Pwd="123"}; this.DataContext = da; } private void Button_Click_1(object sender, RoutedEventArgs e) { da.Name = DateTime.Now.ToString("HH:mm:ss:fff"); } } }
原文地址:https://www.cnblogs.com/w-wz/p/4607052.html