问题
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.
回答1:
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!?
来源:https://stackoverflow.com/questions/39907515/uwp-textbox-text-binding-not-working