2010-09-07 119 views
4

我正在尝试实现一个带有顶部文本框的进度条,该文本框还显示进度%。但百分比是分数。是否可以通过绑定对数据集中返回的值进行四舍五入,还是必须通过后面的代码来完成?四舍五入WPF绑定中的值

<ProgressBar Grid.Row="2" Grid.ColumnSpan="2" Height="25" HorizontalAlignment="Stretch" Margin="5,5,5,2" Name="pbProgressIndex" VerticalAlignment="Top" Width="Auto" Value="{Binding Path=ProgressIndex, Mode=OneWayToSource}" /> 
<TextBlock Grid.Row="2" Grid.ColumnSpan="2" Height="25" Name="txtProgressIndex" Text="{Binding Path=ProgressIndex, Mode=OneWayToSource}" Width="Auto" Foreground="Black" FontWeight="Bold" FontSize="14" FontFamily="Verdana" Padding="5" Margin="5,5,5,5" TextAlignment="Center" /> 

回答

6

使用绑定的StringFormat属性,如:

{Binding Path=ProgressIndex, Mode=OneWayToSource, StringFormat=2N} 
+0

我结束了修改数据集圆时,它会进来,然后使用值: 文本=“{绑定路径= ProgressIndex,模式=单向, StringFormat = {} {0}%}“ – jasonk 2010-09-07 21:35:09

1

除了Femaref的答案,你需要摆脱的Mode=OneWayToSource设置的StringFormat。该模式用于将值从控件推送到绑定对象(如ViewModel),而不接收代码中对值的更新,这与您尝试执行的操作相反。您希望这些OneWay模式恰好是TextBlock.Text的默认模式。 ProgressBar.Value默认使用了TwoWay,在这种情况下仍然可以正常工作,但您也可以将其设置为Mode=OneWay

+0

良好的通话。在回来看到答案(YAY!复制和粘贴)之前,我发现它,但注意细节+1。 – jasonk 2010-09-07 21:01:01

5
StringFormat={}{0:#.00} 

它看起来更好的给我;)