0

I am very new to wpf and xaml and I appreciate your help!... I need to round (to two decimal places) the Hookload STRING representation of a floating point decimal value (example "128.34956483727845956" will display "128.35" on the label). I believe the problem is that I am bringing in a string... Nothing I've tried works.

My current code is as follows:

<TextBlock x:Name="Hookload" Text="{Binding Path=Hookload.Value, StringFormat={}{0:N0}%}"    Background="White"  FontSize="12" FontWeight="Bold" TextBlock.TextAlignment="Center" Margin="17,60,0,0"     HorizontalAlignment="Left" Height="42" Width="138" VerticalAlignment="Top" Padding="0">
            </TextBlock>

I've tried everything I can find to truncate or round to any decimal place using a label and TextBlock:

StringFormat={}{0:N0}%} 
ContentStringFormat="{}{0:N0}%"
StringFormat=N2
StringFormat=N{0}
TextTrimming="CharacterEllipsis"

PLEASE HELP!!!! Thank you!

More attempts from suggestions:

I'm trying to implement the IValueconverter now from this example: Can you limit the length of Text visible in a WPF TextBlock? and adding a custom namespace to xaml

BUT I'm having problem with this:

Error 6 The name "MyTruncateConverter" does not exist in the namespace "clr-namespace:MyTruncateConverter". C:\Projects\FlexView2.0\FlexView\Src\Presentation\DashboardModule\Views\DetailView.xaml 19 9 DashboardModule Error 7 The type 'local:MyTruncateConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. C:\Projects\FlexView2.0\FlexView\Src\Presentation\DashboardModule\Views\DetailView.xaml 19 10 DashboardModule

My code for this attempt is here: The second to last line is where the error occurs.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:AttachedCommand="clr-namespace:WPF.RealTime.Infrastructure.AttachedCommand;assembly=WPF.RealTime.Infrastructure" 
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    xmlns:AttachedProperty="clr-namespace:WPF.RealTime.Infrastructure.AttachedProperty;assembly=WPF.RealTime.Infrastructure" 
    xmlns:System="clr-namespace:System;assembly=mscorlib" 
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"    x:Class="DashboardModule.Views.DetailView" 
    xmlns:local="clr-namespace:MyTruncateConverter"
    Title="Rig Detail" 
    SizeToContent="WidthAndHeight"
    WindowStyle="ToolWindow"
    AttachedProperty:WindowProperties.Left="1"
    AttachedProperty:WindowProperties.Top="1"
    AttachedProperty:WindowProperties.Height="{x:Static   AttachedProperty:WindowProperties.OneHalfHeight}"
    AttachedProperty:WindowProperties.Width="{x:Static AttachedProperty:WindowProperties.HalfWidth}">
<Window.Resources>
    <System:String x:Key="MyData">Lorem ipsum dolor sit amet, consectetur adipiscing elit.blah    blah blah d et auctor nibh. Proin ac ultricies tellus.</System:String>
    <local:MyTruncateConverter x:Key="MyConverter" />
</Window.Resources>

I am clearly lacking knowledge in how to add the class correctly. But I don't know how to fix this!

Point to namespace not class...

xmlns:local="clr-namespace:DashboardModule.Views" 
Community
  • 1
  • 1
  • if `Hookload.Value` is a `string` then you'll need to bind it through custom [`IValueConverter`](http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter(v=vs.110).aspx) which will convert that to `double` – dkozl Oct 14 '14 at 14:28
  • I don't think `StringFormat` will be processed if the source value is already a string, and you can't use numeric format specifiers on a string value anyway. Best to use a custom value converter if you can't simply expose the value as a `float`. – Mike Strobel Oct 14 '14 at 14:29
  • Thank you. I'll edit the question above with my issues with IValueConverter... This is rather simple, but unknown to me... – Richard Howes Oct 14 '14 at 14:42
  • In the line `xmlns:local="clr-namespace:MyTruncateConverter"`, replace `MyTruncateConverter` with the namespace containing the `MyTruncateConverter` class. – Mike Strobel Oct 14 '14 at 14:51
  • you should redesign your viewmodel, I mean the `Hookload.Value` should be a float or double, not a string. Then we don't need any converter. – King King Oct 14 '14 at 14:52
  • @RichardHowes where you have `xmlns:local="clr-namespace:MyTruncateConverter"` instead of _MyTruncateConverter_ you should have namespace where `MyTruncateConverter` is defined and BTW all you need to do in `Convert` method is `return System.Convert.ToDouble(value);` – dkozl Oct 14 '14 at 14:53
  • the namespace was pointing to a class not a namespace: xmlns:local="clr-namespace:DashboardModule.Views" That was easy enough and I'll let you know if the rest of the solution works. – Richard Howes Oct 14 '14 at 14:54
  • King King, All the data comes in from a sql call as "Key, Values," collection and "Values" (Plural) contains a Collection of "Key, Value". – Richard Howes Oct 14 '14 at 14:57
  • Mike Strobel and dkozl! I'm not keeoping up fast enough with the comments, but that was exactly the problem/solution! thank you – Richard Howes Oct 14 '14 at 14:58
  • I'm going to modify the code behind to do the rounding. The other reference does simple replacement but ... If Mike Strobel wants to post his comment as the answer, I'll accept it. Thank you to everyone! – Richard Howes Oct 14 '14 at 19:12

1 Answers1

0

Point to namespace not class...

xmlns:local="clr-namespace:DashboardModule.Views"