Reputation: 4741
I could not target a break point in SET property of View Model, so the default value not changed. (Get - is all right and it initialize my Text Box with valid default value. )
I have a model.cs where a public string field defined
model.cs
{
..
public textDefValue = "aaa";
}
and here is a ViewModel
{
..
Model model = new Model();
....
public string TextField
{
get { return model.textDefValue; }
set
{
//break point here
model.textDefValue = value;
RaisePropertyChanged(TextField);
}
}
....
protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
And XAML:
<TextBox x:Name="myBox" Text="{Binding ViewModel.TextField, Mode=TwoWay}">
I suppose that when I will type something in this Text Box the SET will work and I will target a break point but, I could not hit this break in SET. Where is a bug?
Upvotes: 2
Views: 55
Reputation: 34218
There's no bug, just a misunderstanding.
By default, the binding for a control's .Text
property is only updated when you leave the box (i.e. move focus to a different control). You'd need to click or Tab away for the value to update and the breakpoint to be hit.
You can change this behaviour by updating your binding as follows:
Text="{Binding ViewModel.TextField, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
This will cause the binding to update every time the text value changes - that is, per-keypress in your text box.
Upvotes: 6