<Window x:Class="UserStore.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<!--g1绑定了Person-->
<Grid Name="g1">
<TextBox Height="23" Text="{Binding Name}" HorizontalAlignment="Left" Margin="50,22,0,0" Name="txtName" VerticalAlignment="Top" Width="155" />
<TextBox Height="23" Text="{Binding Age}" HorizontalAlignment="Left" Margin="50,70,0,0" Name="txtAge" VerticalAlignment="Top" Width="155" />
<TextBox Height="23" Text="{Binding Height}" HorizontalAlignment="Left" Margin="50,125,0,0" Name="txtHeight" VerticalAlignment="Top" Width="153" />
<CheckBox Content="性别" IsChecked="{Binding Gender}" Height="16" HorizontalAlignment="Left" Margin="50,178,0,0" Name="chkGender" VerticalAlignment="Top" />
</Grid>
</Window>
//MainWindow.xaml.cs
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.Navigation;
using System.Windows.Shapes; namespace UserStore
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Person p1 = new Person(); public MainWindow()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
p1.Name = "abc";
p1.Age = 26;
p1.Height = 170;
p1.Gender = true; g1.DataContext = p1; } }
}
//Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel; namespace UserStore
{
class Person
{
public int Age
{
get;
set;
}
public string Name
{
get;
set;
}
public int Height
{
get;
set;
}
public bool Gender
{
set;
get;
}
} }