When the least significant values of a double are non-zero, it gets rendered with different precision depending on which control is showing it.
In my case, I tried using a TextBox and a Label. ToString seems to give the same result than a TextBox. However, a Label control shows more precision.
Here's an example (just drag the thumb to see what I mean):
<Window x:Class="SliderTest.SliderTestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xm开发者_如何转开发lns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SliderTestWindow" Height="300" Width="300">
<StackPanel>
<Slider Name="slider" TickFrequency="0.1" IsSnapToTickEnabled="True" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0">Value</TextBlock>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding ElementName=slider, Path=Value}" />
<TextBlock Grid.Row="1" Grid.Column="0">Value</TextBlock>
<Label Grid.Row="1" Grid.Column="1" Content="{Binding ElementName=slider, Path=Value}" />
</Grid>
</StackPanel>
Why is used a different way to render doubles in each control?
What can I do to render doubles in a Label just as they are rendered in a TextBox?
You may provide a format string for the binding. A sample using a currency formatter is:
<TextBox Text="{Binding Path=Double, StringFormat=F3}"/>
<TextBox Text="{Binding Path=Double, StringFormat=Amount: {0:C}}"/>
<TextBox Text="{Binding Path=Double, StringFormat=Amount: \{0:C\}}"/>
<TextBox>
<TextBox.Text>
<Binding Path="Double" StringFormat="{}{0:C}"/>
</TextBox.Text>
</TextBox>
Source: http://blogs.msdn.com/b/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx
String Format help: http://alexonasp.net/samples/stringformatting/
精彩评论