Want to show the value of a slider in a text block?

I am making a Windows Phone 7 application and I am inserting a slider. It starts with 1 and ends with 80. I want it to be, when you stop the slider, it shows the value wherever the slider is in the text block. (for example, if someone stops halfway, the text block will say 40)

+3
source share
4 answers

The easiest way would be to bind the value of the slider property to the Textblocks Text property, and then you will get the value updated every time it is changed. You can also create a converter because the "Sliders" value is double, and then with the converter you can convert this value to Int too.

Text="{Binding ElementName=YourSlider,Path=Value}"
+14

TextBlock.Text Slider.Value.ToString() , , / .

, Math.Round():

TextBlock.Text = Math.Round(Slider.Value, [Number of Decimals]).ToString()

+3

For the slider you should make a changechanged event (something like this)

private void ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{

}

Then in this case you can set the text of the text field →

textbox1.Text = silder1.Value.ToString();

Each time the slider changes, the event fires, so the text field also changes

hope this helps

Bart

0
source

The answer of "Bart Teunissen" is pretty good, but just add a simple check or OldValue is available or not, then it will work perfectly.

private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
   if(slider.OldValue != null)
   {
      textbox1.Text = silder1.Value.ToString();
   }
}

hope this help.

0
source

All Articles