0

I have three texboxes and one slider which changes their Text properties. What i have to do is to bind slider's value property with Text textbox property but in a specific way. When one of textboxes are activated(gotfocused) i need slider to change its Text property. And only that one. I have binded it so far but when i move the slider all textboxes are updated.

Any ideas? I was reading about converters, but i don't see how to implement it within my program.

http://forums.create.msdn.com/forums/t/95548.aspx here you have got code of my slider and textblock.

Whencesoever
  • 2,218
  • 15
  • 26
  • Please don't post your code as an external link. It makes it unsearchable, and also means that to try and help you people have to navigate away from this site or open a new browser window. It also means that if the external site is down for some reason, your question becomes meaningless. Thanks. :) – Ken White Nov 21 '11 at 23:09

1 Answers1

0

What about simply changing the active binding when a textbox receives focus:

Code Behind:

    private Binding _activeBinding;
    private TextBox _activeTextbox;
    private TextBox ActiveTextBox
    {
        get { return _activeTextbox; }
        set
        {
            // Check if a binding exists, initialize if one does not
            if (_activeBinding == null)
            {
                _activeBinding = new Binding("Value");
                _activeBinding.Source = this.sld;
            }

            if (_activeTextbox != null)
            {
                // Clear the binding
                _activeTextbox.ClearValue(TextBox.TextProperty);
            }

            _activeTextbox = value;

            if (_activeTextbox != null)
            {
                // Set the new binding
                _activeTextbox.SetBinding(TextBox.TextProperty, _activeBinding);
            }
        }
    }

    private void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        this.ActiveTextBox = sender as TextBox;
    }

XAML:

 <Grid>
    <StackPanel>
        <TextBox GotFocus="TextBox_GotFocus">1</TextBox>
        <TextBox GotFocus="TextBox_GotFocus">2</TextBox>
        <TextBox GotFocus="TextBox_GotFocus">3</TextBox>
        <Slider x:Name="sld"></Slider>
    </StackPanel>
</Grid>
robowahoo
  • 1,259
  • 9
  • 10
  • Hm, can You help me add this to my program? I should grou that 4 items to stack panel, then add that metods to my mainpage.xaml.cs ? – Whencesoever Nov 21 '11 at 18:49
  • You do not need to add the items to the stack panel, that was just my example code. 1) Add the ActiveTextbox property to your code behind just as in my example (this will handle the binding change). 2) Remove the "Text = {Binding..." from the XAML declaration of your TextBoxes. 3) Change the _GotFocus event handler in you XAML declaration for each TextBox to point to the new method TextBox_GotFocus method I created in my example. In this case my TextBox1, 2, and 3 are stand-ins for your existing textboxes. – robowahoo Nov 21 '11 at 18:58
  • The type or namespace name Binding could not be found. I daleted {Binding..." as you told. Is anything else to do? – Whencesoever Nov 21 '11 at 19:08
  • Try adding using System.Windows.Data; to the top of your mainwindow.xaml.cs – robowahoo Nov 21 '11 at 19:33
  • private Binding _activeBinding; <-- that line – Whencesoever Nov 21 '11 at 19:34
  • """" 'System.Windows.Data.BindingOperations' does not contain a definition for 'ClearBinding'"""" now something like that. – Whencesoever Nov 21 '11 at 19:35
  • Ahh, sorry about that. This should answer your question: http://stackoverflow.com/questions/1639219/clear-binding-in-silverlight-remove-data-binding-from-setbinding Use _activeTextbox.ClearValue(TextBox.TextProperty) directly instead of BindingOperations (apparently that doesn't exist in Silverlight) – robowahoo Nov 21 '11 at 19:38
  • I have same hendler bugs sometimes, but it compiles and i hope i will manage to solve that on my own. Thanks for your help. ;). – Whencesoever Nov 21 '11 at 19:45