Search code examples
vb.netwindows-store-appswindows-store

How to bind values of TextBox and Slider to one another? VB.net (Windows Store)


I want the text in the TextBox to change while I slide the slider, and vice versa (the value of the slider changes as I type in the TextBox). Here is the XAML code for the Slider:

<Slider x:Name="PercentageSlider" HorizontalAlignment="Left" Height="46" Margin="333,319,0,0" VerticalAlignment="Top" Width="788" Foreground="#FF5D9FAD" Minimum="1" Value="{Binding Text, ElementName=PercentageValueTextBox}"/>

And here is for the TextBox:

<TextBox x:Name="PercentageValueTextBox" Height="46" Margin="0,319,161,0" TextWrapping="Wrap"  VerticalAlignment="Top" HorizontalAlignment="Right" Width="69" FontSize="26" Foreground="White" Background="{x:Null}" Text="{Binding Value, ElementName=PercentageSlider}"/>

Solution

  • Your Xaml is correct for linking your two controls, what is causing the problem is the Minimum = 1, that is overriding the ability of the TextBox to effect the Slider.

    Try changing your Binding expressions to:

    TextBox :

       Text="{Binding Value, ElementName=PercentageSlider, Mode=TwoWay}"
    

    Slider :

    Value="{Binding Text, ElementName=PercentageValueTextBox, Mode=TwoWay}"