using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
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;
using WpfApplication5.Annotations;
namespace WpfApplication5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
student stu = new student();
public MainWindow()
{
InitializeComponent();
DataContext = stu;
//Binding binding = new Binding();
//binding.Source = stu;
//binding.Path = new PropertyPath("Name");
//BindingOperations.SetBinding(this.testBox, TextBox.TextProperty, binding);
}
private void test(object sender, RoutedEventArgs e)
{
stu.Name = "fuck";
}
}
class student:INotifyPropertyChanged
{
public student()
{
Name = "fuck";
}
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this,new PropertyChangedEventArgs("Name"));
}
}
}
}
}
<Window x:Class="WpfApplication5.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">
<StackPanel>
<TextBox x:Name ="testBox" Text="{Binding Name}"/>
<Button Height="20" Margin="10" Click="test"/>
</StackPanel>
</Window>
代码和实现都很简单,绑定的语法规范和行为规则也算了解和编写
但是我在单步跟踪的时候,有几个疑问很不解
疑问点(1)
1,在UI的Textbox
中输入后,
2,单步跟踪来到set()
,
3,然后过this.PropertyChanged.Invoke(this,nePropertyChangedEventArgs("Name"));
(这里应该调用委托函数,并迅速返回了)
4,在走完set()
之后,为什么还是要走get()
?
疑问点(2)
1,触发click响应函数
2,单步跟踪来到set()
3,然后过this.PropertyChanged.Invoke(this,nePropertyChangedEventArgs("Name"));
(这里应该调用委托函数,在这个函数内调用了get()
再返回)
(这里为什么在PropertyChanged.Invoke()
函数里面要调用get()
,而上面则不用)
由于本人技术能力有限,本人在对wpf绑定的问题查了很多,也实在没理解,没找到合理的解释,若有幸有大神看到,还望指点一二,谢谢。