Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP TextBox Text binding not working

Tags:

mvvm

uwp

uwp-xaml

I am having some problems with a TextBox.Text binding in UWP. I have been doing WPF for years and typically know what I'm doing in XAML but can't get this binding to work...

I have a TextBox and a Button in the same scope in the XAML

<StackPanel Orientation="Horizontal"
            Margin="0,10,0,0">
    <TextBox Width="200" Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"/>
    <Button Margin="10,0,0,0"
            Command="{Binding SearchBusCommand}">Go</Button>
</StackPanel>

And the bound properties are in the same scope in the ViewModel

public ICommand SearchBusCommand { get; }

public string SearchText { get; set; }

But, when I type text in the TextBox and hit the Button, the command executes and the SearchText value is null...

My expectation is that when I type text into the TextBox the SearchText property is updated with the Text value.

If I set the value of the SearchText property from the ViewModel it does appear in the TextBox.

like image 454
Glen Thomas Avatar asked Jun 26 '26 15:06

Glen Thomas


1 Answers

Ok it seems that in UWP the binding on the TextBox.Text property is OneWay by default..!

I had to set the binding to TwoWay to make it work.

<TextBox Width="200" Text="{Binding SearchText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

WHY WOULD THEY DO THAT!?

like image 188
Glen Thomas Avatar answered Jun 29 '26 05:06

Glen Thomas