13

I have two textboxes which I want to keep synchronized i.e. the content of both the textboxes should be exactly same. If one textbox changes the other textbox content should be synchronized automatically and viceversa. I want to achieve it using WPF databinding facilities. I have the following code:

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

I tried using the Binding Markup Extensions but could not get it right. Here is how I specified Binding on the firstTextBox:

<TextBox x:Name="firstTextBox" Background="Silver" Text="{Binding Source=secondTextBox, Path=Text, Mode=TwoWay}"></TextBox>

Also, there are no runtime errors. What am I doing wrong?

Anand Patel
  • 6,031
  • 11
  • 48
  • 67

2 Answers2

13

If that is what you want to do, WPF will let you do it:

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

The ElementName syntax is a very powerful addition to the basic approach of binding to properties in the DataContext. Many crazy things become possible.

Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
  • Exactly what I wanted. The only difference is, I used Source=secondTextBox and in your code it is ElementName=secondTextBox. Could u please also explain the difference? I marked your response as the answer; it solved my problem. – Anand Patel May 09 '11 at 05:14
  • @Anand Patel: See the documentation for `ElementName`. The remarks describe it and it has an example similar to yours. There is also a link to Binding Overview which is good reading. Here's the link: http://msdn.microsoft.com/en-us/library/system.windows.data.binding.elementname.aspx – Rick Sladkey May 09 '11 at 05:36
  • hi @RickSladkey, is there anyway to synchronize the secondTextBox only when user press the enter key? – Nerdynosaur May 02 '18 at 06:09
1

Are you sure you want to bind one textbox to another textbox? By doing this changing the text value in the other textbox would not affect the other (unless each textbox binds to the other, which sounds like a ridiculous thing to do).

The correct way to achieve this is to have both textboxes (or any number of textboxes, controls...etc) bind to a single Property. This property exists in the data-model and/or view-model.

AlvinfromDiaspar
  • 6,611
  • 13
  • 75
  • 140
  • 1
    How binding two textboxes together is different from binding a textbox and a data model(implementing INotifyPropertyChanged)? I am doing this only for the purpose of learning. There are no Model and ViewModel classes :). I am very clear (as specified) on what I want to achieve. Not clear on how to do that with data binding or is possible with databinding. – Anand Patel May 09 '11 at 04:46