C# 文本框值赢得';t使用数据绑定进行更新
为了避免在评论部分进行长时间的讨论,我再次提出这个问题 我有一个绑定到对象的C# 文本框值赢得';t使用数据绑定进行更新,c#,wpf,xaml,data-binding,textbox,C#,Wpf,Xaml,Data Binding,Textbox,为了避免在评论部分进行长时间的讨论,我再次提出这个问题 我有一个绑定到对象的文本框 更改对象值时,TextBox文本值不会更改 我读了所有关于这一点的讨论,但都没有弄明白 XAML: 我被告知在PropertyChanged上实现。我想是的。如果您要使用主窗口.xaml.cs,那么我建议您定义一个依赖属性来绑定: public static readonly DependencyProperty StudentProperty = DependencyProperty. Register
文本框
更改对象值时,TextBox
文本值不会更改
我读了所有关于这一点的讨论,但都没有弄明白
XAML:
我被告知在PropertyChanged上实现。我想是的。如果您要使用主窗口.xaml.cs
,那么我建议您定义一个依赖属性来绑定:
public static readonly DependencyProperty StudentProperty = DependencyProperty.
Register("Student", typeof(Student), typeof(MainWindow), new
UIPropertyMetadata(100.0));
public Student Student
{
get { return (Student)GetValue(StudentProperty); }
set { SetValue(StudentProperty, value); }
}
然后,您必须设置DataContext
。。。最简单(但不是最好)的方法是在构造函数中执行此操作:
public MainWindow()
{
InitializeComponent();
Student = new Student() { FirstName = "John", OtherProperty = "Something else" };
DataContext = this;
}
现在从Student
对象绑定到一个属性(假设其中有一个名为FirstName
)的属性):
您需要再次设置DataContext
:
private void dgStudents_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
selectedStudent = new student();
DataContext = (student)dgStudents.SelectedItem;
}
您已经在main window
类上实现了INotifyPropertyChanged
,这是正确的,但是您使用的是DataContext
作为代码建议的属性名称(这不起作用,可能是绑定不起作用的原因):
然后,您应该更改Textbox
Text属性绑定,以指向student
对象属性中的属性,如下所示(selectedStudent.FirstName):
OnPropertyChanged(“\u selectedStudent”)代码>应在属性上更改(“selectedStudent”)代码>。在调试中,分配selectedStudent时是否看到调用OnPropertyChanged?firstName是公共财产吗?LPL-已更改。同样的问题。是的,是的。。同样的问题。你调用OnPropertyChanged(“名字”)代码>不带下划线?多种方法(用于改进)。我认为最简单的方法是将Border的DataContext绑定到dgStudents.SelectedItem<我不明白。我什么时候给对象赋值?你可以随时随地给它赋值。。。我更新了我的答案,将其添加到构造函数中。如果我这样做,它解决了问题:txt.dataContext=dg.selectedItem。对于每个文本框。我以为数据绑定是为了防止这样的事情??窗口位于文本框的顶层。如果不设置文本框的DataContext,它将尝试使用父控件=>Grid=>Grid=>TabItem。。。。窗户。在代码中设置this.DataContex时,您正在设置窗口的DataContext,以便其他控件也可以使用它(textBox)。我的代码也应该可以工作。我建议您研究MVVM模式,因为ModelView类应该是视图(窗口)的DataContext上的类。ModelView将属性公开为SelectedStudent,您将从文本框(Text=“{Binding SelectedStudent.FirstName})绑定到此属性。
public MainWindow()
{
InitializeComponent();
Student = new Student() { FirstName = "John", OtherProperty = "Something else" };
DataContext = this;
}
<TextBox Text="{Binding Student.FirstName}" />
private void dgStudents_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
selectedStudent = new student();
DataContext = (student)dgStudents.SelectedItem;
}
//Below DataContext binding needs to be changed, you are binding directly to property, instead you should bind to you MainWindow object
public MainWindow()
{
InitializeComponent();
selectedStudent = clsLoadStudent();
//this.DataContext = selectedStudent; // Does not work
this.DataContext = this; // Correct way
}
<TextBox Height="36" HorizontalAlignment="Left" Margin="174,0,0,400" Name="txtStudenName" VerticalAlignment="Bottom" Width="240" IsEnabled="False" Text={Binding selectedStudent.FirstName, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}/>
OnPropertyChanged("selectedStudent");