How to make TextBlock.Text = Slider.Value on slider value change

17,815

You don't need to do this in Codebehind just bind to your value in XAML and if you want to modify that value somehow, then use a ValueConverter.

see http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

  <Slider x:Name="mySlider"/>
  <TextBox x:Name="myTextBox" Text="{Binding ElementName=mySlider,Path=Value}"/>

Otherwise if you definitely want to do it in CodeBehind use the ValueChangedEvent:

XAML:

  <Slider x:Name="mySlider" ValueChanged="mySlider_ValueChanged"/>

CodeBehind

Private Sub mySlider_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double))
    myTextBox.Text = e.NewValue.ToString()
End Sub
Share:
17,815
happycamper1221
Author by

happycamper1221

Updated on June 04, 2022

Comments

  • happycamper1221
    happycamper1221 over 1 year

    I've tried this

       If Slider1.Value = 1 Then
            TextBlock1.Text = "1"
        End If
        If Slider1.Value = 2 Then
            TextBlock1.Text = "2"
        End If
        If Slider1.Value = 3 Then
            TextBlock1.Text = "3"
        End If
        If Slider1.Value = 4 Then
            TextBlock1.Text = "4"
        End If
        If Slider1.Value = 5 Then
            TextBlock1.Text = "5"
        End If
    

    I actually get a couple of errors with this, especially when i have the

        If Slider1.Value = 1 Then
        TextBlock1.Text = "1"
        End If
    

    code because it's already on value 1 when the program runs. I'm new to WPF and don't really know what to do here so i you could please show me or provide a code sample on how it's done thanks.

    and also i have been using expression blend 4 for a couple of days and i know how to create a template and animate fades on mouse over and stuff for a button but say if the user clicks a button how would i animate a separate picturebox or image to fade in or fade out could you please provide me an example thankyou everyone :).