【发布时间】:2021-10-10 16:24:43
【问题描述】:
我有一个初学者问题。也许我很困惑,并没有完全弄清楚绑定概念。
我尝试使用绑定,但是当我更改源值时,我在文本框上看不到任何更新。
XLAM 文件:
<TextBox IsReadOnlyCaretVisible="True" AutomationProperties.Name="keyIdTextBox" Text="{Binding Path=Name, Mode=oneWay}"/>
...
<Button Content="Open" AutomationProperties.Name="openCloseButton" Click="OpenClose_Click"/>
C# 文件:
public class Person
{
private string nameValue;
public string Name
{
get { return nameValue; }
set { nameValue = value; }
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Person person = new Person { Name = "Bob" };
public MainWindow()
{
InitializeComponent();
this.DataContext = person;
}
private void OpenClose_Click(object sender, RoutedEventArgs e)
{
person.Name = "Lenny";
}
在开始时,文本框更新良好并显示“Bob”,但是当我单击按钮更新名称的值时,文本框不反映值“Lenny”。没有变化。
感谢您的帮助。
【问题讨论】:
标签: wpf data-binding