如何将TextBox和Slider的值绑定到另一个? VB.net(Windows Store)

时间:2013-01-28 18:15:14

标签: vb.net windows-store-apps windows-store

我想在滑动滑块时更改TextBox中的文本,反之亦然(当我在TextBox中键入时,滑块的值会发生变化)。 以下是Slider的XAML代码:

<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}"/>

这里是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}"/>

2 个答案:

答案 0 :(得分:1)

您的Xaml对于关联两个控件是正确的,导致问题的原因是Minimum = 1,这会覆盖TextBox影响Slider的能力

尝试将Binding表达式更改为:

TextBox:

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

滑块:

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

答案 1 :(得分:0)

为文本框:

textchanged event:
slider1.value=CInt(textbox1.text)
滑块的

valuechanged event:
textbox1.text=slider1.value.tostring()

此处文本框为textbox1,滑块为slider1