我有两个文本框,我想保持同步,即两个文本框的内容应该完全相同.如果一个文本框更改其他文本框内容应自动同步,反之亦然.我想使用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>
Run Code Online (Sandbox Code Playgroud)
我尝试使用Binding Markup Extensions但无法正确使用它.这是我在firstTextBox上指定Binding的方法:
<TextBox x:Name="firstTextBox" Background="Silver" Text="{Binding Source=secondTextBox, Path=Text, Mode=TwoWay}"></TextBox>
Run Code Online (Sandbox Code Playgroud)
此外,没有运行时错误.我究竟做错了什么?
Ric*_*key 10
如果这是你想要做的,WPF将允许你这样做:
<Grid>
<StackPanel>
<TextBox x:Name="firstTextBox" Background="Silver" Text="{Binding Path=Text, ElementName=secondTextBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBox x:Name="secondTextBox" Background="Gold" ></TextBox>
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
该ElementName语法是一个非常强大的附加功能结合在属性的基本方法DataContext.许多疯狂的事情变得可能.
| 归档时间: |
|
| 查看次数: |
8462 次 |
| 最近记录: |