Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding a Value in the WPF Binding

I'm attempting to implement a progress bar with a textbox on top that also displays the progress %. However the percentage is fractional. Is it possible to round a value returned in the dataset via the binding or does it have to be done via the code behind?

<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" />
like image 685
jasonk Avatar asked Dec 16 '22 21:12

jasonk


1 Answers

Use the StringFormat Property of the Binding, eg.:

{Binding Path=ProgressIndex, Mode=OneWayToSource, StringFormat=N2}

The N2 formatting can also be specified the following way:

{}{0:N2}}
like image 103
Femaref Avatar answered Dec 31 '22 01:12

Femaref