这是与 flq 的答案一起使用的代码:
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _text1;
private string _text2;
public string Text1
{
get { return _text1; }
set
{
if (_text1 != value)
{
_text1 = value;
RaisePropertyChanged("Text1");
Text2 = _text1;
}
}
}
public string Text2
{
get { return _text2; }
set
{
if (_text2 != value)
{
_text2 = value;
RaisePropertyChanged("Text2");
}
}
}
public MyViewModel()
{
}
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
请务必将绑定更改为双向。
编辑:
这是 XAML:
<TextBox Text="{Binding Text1, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Text="{Binding Text2}" />
设置 UpdateSourceTrigger=PropertyChanged 允许在您键入时更新属性,因此 TextBox2 将在您键入时更新。(仅供参考 - TextBoxes 的默认绑定是双向的)