大佬们为什么textbox绑定了数据源对象..每次修改数据源第一次修改的时候 UI都不更新...只有第二次以后修改UI才会更新,第一次点击按钮的时候运行Button_Click 数据源b的Name已经改变了..但是UI没有更新,只有第二次点击按钮后UI才会更新,这是为什么..?
public partial class Window1 : Window
{
public B b = new B();
public Window1()
{
InitializeComponent();
var bd = new Binding();
bd.Source = b;
bd.Mode = BindingMode.OneWay;
bd.Path = new PropertyPath("Name");
textbox1.SetBinding(TextBox.TextProperty, bd);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
b.Name = "test";
}
}
public class B : INotifyPropertyChanged
{
string name;
public string Name
{
get => name;
set { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Name")); name = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
}
public partial class Window1 : Window
{
public B b = new B();
public Window1()
{
InitializeComponent();
var bd = new Binding();
bd.Source = b;
bd.Mode = BindingMode.OneWay;
bd.Path = new PropertyPath("Name");
textbox1.SetBinding(TextBox.TextProperty, bd);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
b.Name = "test";
}
}
public class B : INotifyPropertyChanged
{
string name;
public string Name
{
get => name;
set { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Name")); name = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
}