0

I have a Slider control and a TextBlock that shows the value of the Slider via data binding.

The problem is that it displays a float and I'm interested in an integer.

I want to cast the float to an integer so instead of seeing 45.25139664804483 in the TextBlock, I'll just see 45.

9
  • 1
    Did you try something like int newValue = Convert.ToInt32(floatValue);? Commented Mar 19, 2013 at 4:42
  • Yes, Tim is right. Refer - stackoverflow.com/questions/2578348/… Commented Mar 19, 2013 at 4:42
  • @Tim I did, but the UI didn't know when the value was updated. It started at 0, and after the slider's value changed, the TextBlock's text stayed at 0. Commented Mar 19, 2013 at 4:42
  • Ah...that's a different question. You need to update the TextBox contents, probably with an event handler. Post your code and you'll get good answers. Heh - like the ones below that came in while I was typing this comment the first time :) Commented Mar 19, 2013 at 4:45
  • Oh, I aim for a solution that involves data-binding. Is this possible? Commented Mar 19, 2013 at 4:47

3 Answers 3

3

You can use StringFormat on your TextBlock binding to format away the decimal places

<StackPanel> <TextBlock Text="{Binding Value, ElementName=slider/> <TextBlock Text="{Binding Value, ElementName=slider, StringFormat={}{0:0}}" /> <Slider x:Name="slider" /> </StackPanel> 

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

God I've tried using the StringFormat so many times but couldn't figure out what to write after the colon. Thanks!
yeah, StringFormat in xaml is horrible to use, the trick I have fornd is to start it with {} seems keep intelisense from flipping out ,LOL, oh and anothet trick is if you need to use NewLine or some othet escape char like that use its Hex value e.g: NewLine = &#x0a;
@sa_ddam213 SO should have a "Stalk" feature, so I can stalk your answers and learn more lol
@failedprogramming, you can access peoples answers in thier profile, I stalk a lot of people, LOL
2

Try this on Slider_ValueChanged

textBox.Value = (int)Math.Ceiling(45.25139664804483); 

1 Comment

Isn't there a way to do this with Data Binding?
0
 private void Slider_ValueChanged_1(object sender, RoutedPropertyChangedEventArgs<double> e) { myTextBlock.Text = Convert.ToInt32(Math.Floor(e.NewValue)); } 

Hope this helps. With Binding:

int _myValue; public int MyValue{get{return _myValue;}set{_myValue = value; NotifyPropertyChanged("MyValue");}} private void Slider_ValueChanged_1(object sender, RoutedPropertyChangedEventArgs<double> e) { MyValue = Convert.ToInt32(Math.Floor(e.NewValue)); } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } 

in this case your class needs to implement INotifyPropertyChanged interface

2 Comments

Isn't there a way to do this with Data Binding?
Bind your TextBlock to MyValue. You may wanna see the INotifyPropertyChanged.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.