How can I make a data binding update as soon as a new character is typed in a TextBox?
I'm learning about bindings in WPF and now I've become stuck on a (hopefully) simple matter.
I have a simple FileLister class where you can set a Path property, and then it will give you a listing of files when you access the FileNames property. Here is that class:
class FileLister:INotifyPropertyChanged { private string _path = ""; public string Path { get { return _path; } set { if (_path.Equals(value)) return; _path = value; OnPropertyChanged("Path"); OnPropertyChanged("FileNames"); } } public List<String> FileNames { get { return getListing(Path); } } private List<string> getListing(string path) { DirectoryInfo dir = new DirectoryInfo(path); List<string> result = new List<string>(); if (!dir.Exists) return result; foreach (FileInfo fi in dir.GetFiles()) { result.Add(fi.Name); } return result; } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string property) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(property)); } } }
I'm using the the FileLister as a StaticResource in this very simple app:
<Window x:Class="WpfTest4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfTest4" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <local:FileLister x:Key="fileLister" Path="d:\temp" /> </Window.Resources> <Grid> <TextBox Text="{Binding Source={StaticResource fileLister}, Path=Path, Mode=TwoWay}" Height="25" Margin="12,12,12,0" VerticalAlignment="Top" /> <ListBox Margin="12,43,12,12" Name="listBox1" ItemsSource="{Binding Source={StaticResource ResourceKey=fileLister}, Path=FileNames}"/> </Grid> </Window>
The binding is working. If I change the value in the textbox and then click outside of it, the listbox contents will update (as long as the path exists).
The problem is that I would like to update as soon as a new character is typed, and not wait until the textbox lose focus.
How can I do that? Is there a way to do this directly in the xaml, or do I have to handle TextChanged or TextInput events on the box?
One-Way Data Binding First of all, create a new WPF project with the name WPFDataBinding. The following XAML code creates two labels, two textboxes, and one button and initializes them with some properties.
In One Way binding, source control updates the target control, which means if you change the value of the source control, it will update the value of the target control. So, in our example, if we change the value of the slider control, it will update the textbox value, as shown below.
Two way binding is used when we want to update some controls property when some other related controls property change and when source property change the actual control also updates its property.
In your textbox binding, all you have to do is set UpdateSourceTrigger=PropertyChanged
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With