我有两个文本框,我想让它们保持同步,即两个文本框的内容应该完全相同。如果一个文本框更改了,另一个文本框的内容应该自动同步,反之亦然。我想使用WPF数据绑定功能来实现它。我有以下代码:
<Window x:Class="WPFLearning.DataBindingTwoWay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DataBindingTwoWay" Height="300" Width="300">
<Grid>
<StackPanel>
<TextBox x:Name="firstTextBox" Background="Silver"></TextBox>
<TextBox x:Name="secondTextBox" Background="Gold" ></TextBox>
</StackPanel>
</Grid>
</Window>
我尝试使用Binding Markup Extensions,但无法正确使用。这是我如何在第一个文本框中指定绑定的方法:
<TextBox x:Name="firstTextBox" Background="Silver" Text="{Binding Source=secondTextBox, Path=Text, Mode=TwoWay}"></TextBox>
此外,没有运行时错误。我做错了什么?