我在使用 UWP 中的 TextBox.Text 绑定(bind)时遇到了一些问题。我从事 WPF 多年,通常知道我在 XAML 中做什么,但无法使此绑定(bind)起作用...
我在 XAML 的相同范围内有一个 TextBox 和一个 Button
<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>
并且绑定(bind)的属性在 ViewModel 中处于相同的范围内
public ICommand SearchBusCommand { get; }
public string SearchText { get; set; }
但是,当我在 TextBox 中键入文本并点击 Button 时,命令会执行并且 SearchText 值为 null...
我的预期是,当我在 TextBox 中键入文本时,SearchText 属性会更新为 Text 值。
如果我从 ViewModel 设置 SearchText 属性的值,它会出现在 TextBox 中。
最佳答案
好吧,在 UWP 中,TextBox.Text 属性上的绑定(bind)默认情况下是 OneWay..!
我必须将绑定(bind)设置为 TwoWay 才能使其正常工作。
<TextBox Width="200" Text="{Binding SearchText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
他们为什么要那样做!?
关于mvvm - UWP TextBox 文本绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39907515/