UWP TextBox 文本绑定不起作用 [英] UWP TextBox Text binding not working

查看:26
本文介绍了UWP TextBox 文本绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UWP 中遇到了 TextBox.Text 绑定的一些问题.我多年来一直在做 WPF,通常知道我在 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...

我在 XAML 中的同一范围内有一个 TextBox 和一个 Button

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>

和绑定的属性在ViewModel中的作用域相同

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

public ICommand SearchBusCommand { get; }

public string SearchText { get; set; }

但是,当我在 TextBox 中输入文本并点击按钮时,命令会执行并且 SearchText 值为空...

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

我的期望是,当我在 TextBox 中输入文本时,SearchText 属性会更新为 Text 值.

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

如果我从 ViewModel 设置 SearchText 属性的值,它会出现在 TextBox 中.

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

推荐答案

好吧,似乎在 UWP 中,TextBox.Text 属性上的绑定默认是 OneWay ..!

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

我必须将绑定设置为 TwoWay 才能使其工作.

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!?

这篇关于UWP TextBox 文本绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆