UWP TextBox Text binding not working

主宰稳场 提交于 2019-12-11 01:35:58

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!