后台代码
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 Wpf数据绑定 { /// <summary> /// Window2.xaml 的交互逻辑 /// </summary> public partial class Window2 : Window { User1 us; public Window2() { InitializeComponent(); us = new User1() { Username ="xiao", Pass ="123" }; this.DataContext = us; // } private void Button_Click_1(object sender, RoutedEventArgs e) { //user.Username = DateTime.Now.ToString("HH:mm:ss:fff"); //user.Password = DateTime.Now.ToString("HH:mm:ss"); MessageBox.Show(us.Pass); } } }
前台代码,注意黑色字体
<Window x:Class="Wpf数据绑定.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window2" Height="371.642" Width="471.642"> <Grid Margin="0,0,2,0"> <Label Content="{Binding Username}" HorizontalAlignment="Left" Margin="147,53,0,0" VerticalAlignment="Top" Width="256" Height="57"/> <TextBox Name="pw" HorizontalAlignment="Left" Height="50" Margin="132,115,0,0" TextWrapping="Wrap" Text="{Binding Pass}" VerticalAlignment="Top" Width="230"/> <Button Content="Button" HorizontalAlignment="Left" Margin="280,264,0,0" VerticalAlignment="Top" Width="123" Height="35" Click="Button_Click_1"/> <TextBox HorizontalAlignment="Left" Height="52" Margin="132,184,0,0" TextWrapping="Wrap" Text="{Binding Text,ElementName=pw,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="230"/> </Grid> </Window>
时间: 2024-10-25 08:02:52