代码如下:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Slider Name="mySlider" ValueChanged="mySlider_ValueChanged" Thumb.DragStarted ="mySlider_DragStarted"
Thumb.DragCompleted ="mySlider_DragCompleted"/>
<TextBox Name="tboxValue" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Text="0"
Grid.Row="1"/>
<Button Content="Add" Name="btnAdd" Click="btnAdd_Click" Grid.Row="2"/>
</Grid>
private bool isdragging = false;
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
mySlider.Value += 1;
}
private void mySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (isdragging)
tboxValue.Text = mySlider.Value.ToString();
}
private void mySlider_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
isdragging = true;
}
private void mySlider_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
isdragging = false;
}
实现了一个简单的功能。
利用一个Textbox来显示Slider的值,但是值仅在拖拽Slider的时候更新,在其他情况下改变Slider的值的时候Textbox的内容不发生变化。