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

本文关键字:字符串 数据绑定 存储 xaml 四舍五入 | 更新日期: 2023-09-27 18:26:25

我是wpf和xaml的新手,非常感谢您的帮助!。。。我需要四舍五入(小数点后两位)一个浮点十进制值的Hookload STRING表示(例如"128.34956483727845956"将在标签上显示"128.35")。我相信问题是我带了一根绳子。。。我试过的都不管用。

我当前的代码如下:

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

我已经尝试了使用标签和TextBlock:来截断或舍入到任何小数点

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

请帮忙!!!!非常感谢。

来自建议的更多尝试:

我现在正试图从这个例子中实现IValueconverter:你能限制WPF TextBlock中可见的Text的长度吗?和向xaml 添加自定义命名空间

但我有问题:

错误6名称"MyTruncateConverter"不存在于命名空间"clr namespace:MyTtruncateConverters"中。C: ''Projects''FlexView2.0''FlexView''Src''PPresentation''DashboardModule''Views''DetailView.xaml 19 9 DashboardModule错误7找不到类型"local:MyTtruncateConverter"。请验证是否缺少程序集引用,以及是否已生成所有引用的程序集。C: ''Projects''FlexView2.0''FlexView''Src''PPresentation''DashboardModule''Views''DetailView.xaml 19 10 DashboardModule

我这次尝试的代码如下:倒数第二行是错误发生的地方。

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

我显然不知道如何正确地增加班级。但我不知道该怎么解决!

指向命名空间而不是类。。。

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

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

指向命名空间而非类。。。

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