我在使用两种方式将 TextBox 内容绑定(bind)到另一个类中的属性时遇到问题。搜索 stackoverflow 给出了很多提示/解决方案,但似乎都没有用。
在我的 XAML 代码中我有:
< TextBox ... Width="336" IsReadOnly="True"
Text="{Binding Path=AssignedClearProgram, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
(...我删除了所有不重要的项目)
在附带的cs代码中我有:
public CombiWindow(Combi combi)
{
ViewModel = new CombiViewModel(combi);
DataContext = ViewModel;
}
在 CombiViewModel 中:
[UsedImplicitly]
public string AssignedClearProgram { get; set; }
似乎我第一次分配 AssignedClearProgram 时,文本框填充了我设置的文本,但是在显示窗口并从代码更新 AssignedClearProgram 后(即调用 set 方法),数据不是在屏幕上更新。
当这个变量改变时,有人有更新文本框的解决方案吗?
亲切的问候,
米歇尔
最佳答案
您的 View 模型类需要实现 INotifyPropertyChanged 并且您需要在更改属性时引发该接口(interface)的事件。然后绑定(bind)将发现更改并更新文本框。
关于c# - TextBox.Text 绑定(bind)到 ViewModel 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11596161/