. net在文本框或标签(WPF)中显示时的双重呈现方式不同



当双精度对象的最低有效值非零时,根据显示它的控件,它将以不同的精度呈现。

在我的例子中,我尝试使用文本框和标签。ToString似乎给出了与TextBox相同的结果。但是,Label控件显示出更高的精度。

这里有一个例子(只要拖动拇指就知道我的意思了):

<Window x:Class="SliderTest.SliderTestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns: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>

为什么在每个控件中使用不同的方式来渲染double ?

我怎么做才能在标签中呈现双精度,就像在文本框中呈现一样?

您可以为绑定提供格式字符串。使用货币格式化程序的示例如下:

<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>

来源:http://blogs.msdn.com/b/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx

String格式帮助:http://alexonasp.net/samples/stringformatting/

最新更新