简体   繁体   English

如何从XAML中的数据绑定源舍入存储在字符串中的浮点值

[英]How can I round a float value stored in a string from a data bound source in xaml

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). 我是wpf和xaml的新手,感谢您的帮助!...我需要将(四舍五入到小数点后两位)浮点十进制值的Hookload STRING表示形式(示例“ 128.34956483727845956”将在标签上显示“ 128.35” )。 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: 我已经尝试过使用标签和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? 我现在尝试通过此示例实现IValueconverter:是否可以限制WPF TextBlock中可见的Text的长度? and adding a custom namespace to xaml 并将自定义名称空间添加到xaml

BUT I'm having problem with this: 但是我有这个问题:

Error 6 The name "MyTruncateConverter" does not exist in the namespace "clr-namespace:MyTruncateConverter". 错误6名称空间“ clr-namespace:MyTruncateConverter”中不存在名称“ MyTruncateConverter”。 C:\\Projects\\FlexView2.0\\FlexView\\Src\\Presentation\\DashboardModule\\Views\\DetailView.xaml 19 9 DashboardModule Error 7 The type 'local:MyTruncateConverter' was not found. C:\\ Projects \\ FlexView2.0 \\ FlexView \\ Src \\ Presentation \\ DashboardModule \\ Views \\ DetailView.xaml 19 9 DashboardModule错误7找不到类型'local:MyTruncateConverter'。 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 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" 

Point to namespace not class... 指向名称空间而不是类...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM