[英]WPF Textbox Text Update
(I know there are questions asked about this, but as you can see in my question, I think I tried the solutions without success). (我知道有人对此提出疑问,但是正如您在我的问题中所看到的那样,我认为我尝试解决方案没有成功)。
Simply, I'm trying to bind the text in my textbox with the view model. 简而言之,我试图将文本框中的文本与视图模型绑定在一起。
Code in view model: 视图模型中的代码:
public class MainClass :INotifyPropertyChanged
{
...
public event PropertyChangedEventHandler PropertyChanged;
protected void onPropertyChanged (string propertyName)
{
var handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private string _Stuff ="Original Value";
public string Stuff {
get { return _Stuff; }
private set {
if (value != _Stuff)
{
_Stuff = value;
Method();
OnPropertyChanged("Stuff");
}
}
}
}
And the XAML 还有XAML
<TextBox Text="{Binding Stuff,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</TextBox>
Now, the binding does work one way - that is, when the XAML is first initialized, the text box is populated with "Original Value". 现在,绑定确实以一种方式起作用-即,首次初始化XAML时,在文本框中填充“原始值”。 But when I change the value in the box (even when I lose focus on it), the field isn't updated (I can tell because, firstly, Method isn't called, and also when I use the debugger I can see the value hasn't changed). 但是,当我更改框中的值时(即使我失去对它的关注),该字段也不会更新(我可以知道,因为首先没有调用Method,而且当我使用调试器时也可以看到值未更改)。
What part am I doing incorrectly? 我做错了什么部分?
Edit: I forgot to mention that the context is set in the controller, and is apparently working, because as I said, the binding is working one way. 编辑:我忘了提到上下文是在控制器中设置的,并且显然是有效的,因为正如我所说,绑定是一种工作方式。
Your problem might be here: 您的问题可能在这里:
public string Stuff {
get { return _Stuff; }
private set {
if (value != _Stuff)
{
_Stuff = value;
Method();
OnPropertyChanged("Stuff");
}
}
}
Your setter is private
, remove the private
and it should work. 您的设置员是private
,请删除private
,它应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.