开始更新一点有意思的了
首先 数据绑定 其中之一 Element 绑定
看例子
<Window x:Class="WpfApplication20.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="156.383" Width="246.489" x:Name="MWindow">
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" Margin="30,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding ElementName=MWindow,Path=PresonName}"/>
<TextBlock HorizontalAlignment="Left" Margin="30,16,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="42" Text="部门:"/>
<Button Content="提交" HorizontalAlignment="Left" Margin="30,85,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button Content="重置" HorizontalAlignment="Left" Margin="127,85,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<TextBlock HorizontalAlignment="Left" Margin="85,16,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding ElementName=MWindow,Path=Department}"/>
</Grid>
</Window>
public partial class MainWindow : Window,INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
}
private string department;
public string Department
{
get { return "软件开发"; }
}
private string presonName;
public string PresonName
{
get { return presonName; }
set { presonName = value;
OnPropertyChanged("PresonName");
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hi," + PresonName);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
PresonName = "";
}
#region INotifyPropertyChanged 成员
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if(PropertyChanged!=null){
PropertyChanged.Invoke(this,new PropertyChangedEventArgs(name));
}
}
#endregion
}
我们在这个例子里有俩个操作
一个是数据绑定
一个是数据通知
绑定 是用Element 指定window 窗体 因为 创建属性以后 window 窗体就有这个属性 通过名字.属性的方式
通知 需要继承 INotifyPropertyChanged
实现接口方法
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if(PropertyChanged!=null){
PropertyChanged.Invoke(this,new PropertyChangedEventArgs(name));
}
}
在需要通知的属性下面 加上
OnPropertyChanged(“”属性名“”);
就可以起到通知作用