111,076
社区成员




<Window.Resources>
<local:Person x:Key="Tom" Name="Tom" Age="9" />
</Window.Resources>
.....
<Grid DataContext="{StaticResource Tom}">
.....
<TextBox Text="{Binding Age}" />
<ObjectDataProvider x:Key="myDataSource" ObjectType="{x:Type src:Person}" IsAsynchronous="True" />
<TextBox>
<Binding Source="{StaticResource myDataSource}" Path="Name"/>
</TextBox>
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="winMain"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<TextBox x:Name="txt" Text="{Binding Name}" Width="200" Height="20"></TextBox>
<Button Width="80" Height="20" Content="Button" Click="Button_Click" ></Button>
</StackPanel>
</Grid>
</Window>
public partial class MainWindow : Window
{
private Person _Person = null;
public Person Person
{
get { return this._Person; }
set { this._Person = value; }
}
public MainWindow()
{
InitializeComponent();
this._Person = new Person() { Name = "123" };
this.txt.DataContext = this.Person;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this._Person.Name = "456";
}
}
public class Person : INotifyPropertyChanged
{
private string _Name;
public string Name
{
get { return this._Name; }
set
{
this._Name = value;
this.OnPropertyChanged("Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}