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