0

Okay so I know I can fix this with some string formatting on the textbox, but it's bugging me why this is happening in the first place. I have a slider control, which is going to get its max, min, and precision values from the viewmodel, depending on which page it's loaded into.

As an example, I've hard coded these values.

    <DockPanel VerticalAlignment="Center"
               Margin="40,0,0,0"
               Height="100">
        <TextBox Text="{Binding ElementName=Slider, Path=Value, UpdateSourceTrigger=PropertyChanged}"
                 DockPanel.Dock="Top"
                 TextAlignment="Right"
                 Width="634"
                 Margin="0,0,0,20" />
        <Slider DockPanel.Dock="Bottom"
                Name="Slider"
                Width="634"
                Padding="0"
                Maximum="5000"
                Minimum="-5000"
                SmallChange="0.01"
                LargeChange="0.01"
                TickFrequency="0.01"
                SnapsToDevicePixels="True"
                IsSnapToTickEnabled="True" />
    </DockPanel>

I know I probably don't need a few of those properties, but threw them in trying to fix this.

For the most part this has worked, and the slider is being constrained to two decimal places. But I've noticed (especially with large ranged) that I'm still getting random values which are ignoring this. For example when I set a range of -5000 to 5000 I still randomly get values like 80.20000002.

I know I can apply a string format to the textbox to resolve the issue, (although a tad annoying with the viewmodel setting the precision) just wondering why it's happening.

As an aside, users also need to be able to set the value using a keyboard, again this works perfectly, but I'm wondering if there's a way to make the slider textbox acceppt a decimal point without having a number behind it.

user3265613
  • 355
  • 2
  • 14
  • 2
    *"the slider is being constrained to two decimal place"* - it's not *constrained*, because it uses `double` to store `Value` and [double is not precise](http://stackoverflow.com/q/4252917/1997232) as you may think. Using powers of 2 it can't represent `80.2` as `80.2`, but as `80.200...2`. You must use `StringFormat` if you are displaying `double` somewhere.. – Sinatr Feb 09 '17 at 09:59
  • Ah that's a shame / pain. I was hoping that by setting the ticks to 0.01 and setting snap to tick would prevent the odd values. But if it's just a double precision thing not much I can do about it. Thanks for the quick response. Now I just need to figure out setting the strongformat from the VM. – user3265613 Feb 09 '17 at 10:06
  • 1
    There is [StringFormat](https://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.stringformat(v=vs.110).aspx) in the bindings. Other options are converter or another property in ViewModel returning `string`. – Sinatr Feb 09 '17 at 10:09
  • Because i'm not all that experienced with xaml I've gone with the binding behind. When the value of the slider is updated, calls the method which truncates down to x places, which updates slider value (which the textbox is bound to). It seems to work okay. Thanks for the help. – user3265613 Feb 09 '17 at 12:57

0 Answers0