How do I bind data to two user interface elements so that they can immediately update each other in Silverlight?

Suppose I want to bind two TextBoxes. I can do this in XAML:

<TextBox x:Name="FirstBox" Text="{Binding Text, Mode=TwoWay, ElementName= SecondBox}"></TextBox>
<TextBox x:Name="SecondBox"></TextBox>

Or, I can do it programmatically.

    Binding binding = new Binding("Text");
    binding.Mode = BindingMode.TwoWay;
    binding.Source = SecondBox;
    FirstBox.SetBinding(TextBox.TextProperty, binding);

The problem is that when I type something in SecondBox, it immediately reflects in FirstBox. However, input in the FirstBox does not appear immediately in the SecondBox. SecondBox is updated only when focusing FirstBox.

Of course, I can create another Binding for the SecondBox. But is there any other way to make changes to the FirstBox to immediately update the SecondBox?

+3
source share
3 answers

, , , , . SecondBox ( , , )?

+3

. Xaml :

<TextBox x:Name="tb1" Text="{Binding Path=Text, ElementName=tb2, Mode=TwoWay, UpdateSourceTrigger=Explicit}" TextChanged="tb1_TextChanged" />
<TextBox x:Name="tb2" />

, , , "UpdateSourceTrigger = Explicit", TextChanged. TextBox.Text , TextBox . "UpdateSourceTrigger = Explicit", , , . , TextChanged, :

private void tb1_TextChanged(object sender, TextChangedEventArgs e)
{
    tb1.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
+2

, TextBox. http://msdn.microsoft.com/en-us/library/cc278072(VS.95).aspx

TwoWay , Text Text. , TextBox .

I think you also need to bind the Text property in the second TextBox to get the desired behavior, as Anthony suggested.

+1
source

Source: https://habr.com/ru/post/1716403/


All Articles