RayOldProf RayOldProf - 1 year ago 78
C# Question

Wpf TextBox databinding update

I have a TextBox and it is binded to a string property:

TextBox:

<TextBox x:Name="SearchBox"
Text="{Binding SearchText, Source={StaticResource SearchEngine}, Mode=TwoWay}"
</TextBox>


and the class which is binded to:

public class SearchEngine : ViewModelBase
{
private string _searchText = string.Empty;

public SearchEngine() {
Console.Out.WriteLine("this = {0}", this);
SearchText = "Search";
}

public string SearchText
{
get { return _searchText; }
set
{
if (_searchText != value)
{
_searchText = value;
Console.Out.WriteLine("AAAAAAAAAAAA: " + value);
NotifyPropertyChanged();
}
}
}
}


The problem is that when I type in the TextBox, the
SearchText
doesn't get updated.

Answer Source

You have to use the updatesourcetrigger in your xaml like:

<TextBox x:Name="SearchBox" Text="{Binding SearchText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged"/>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download