【问题标题】:Text formatting in Datagrid cellDatagrid 单元格中的文本格式
【发布时间】:2018-09-10 14:45:41
【问题描述】:

我正在尝试格式化 datagrid 数据单元格中的数字,以使 '1000000.52' 看起来像:'1 000 000.52'

我试图寻找启用此类功能的属性,但只能在 DataGridCell 类中找到 ContentStringFormat 属性。不幸的是,我尝试的方法不起作用:

<DataGrid  CanUserAddRows="False" Name="EGrid" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding Path=Data, Mode=TwoWay, ElementName=ExportableGrid}">
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="ContentStringFormat" Value="## ### ###.00"/>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>

我怎样才能做到这一点?任何帮助将不胜感激,在此先感谢。

【问题讨论】:

  • 我认为解决这个问题的正确方法是将您的双精度转换为字符串,然后对其进行处理以实现您所需要的。

标签: .net wpf vb.net xaml


【解决方案1】:

我会创建一个自定义值转换器:

    Imports System.Globalization

Public Class DecimalFormatConverter
    Implements IValueConverter

    Public Function IValueConverter_Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
        Dim decimalVal As Decimal = Nothing

        If value IsNot Nothing AndAlso parameter IsNot Nothing AndAlso Decimal.TryParse(value.ToString(), decimalVal) Then
            Return decimalVal.ToString(parameter.ToString())
        End If

        Return value
    End Function

    Public Function IValueConverter_ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException
    End Function

End Class

在 xaml 中使用它:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp6VB"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <Grid>
        <Grid.Resources>
            <local:DecimalFormatConverter x:Key="DecimalFormatConverter" />
        </Grid.Resources>
        <DataGrid  CanUserAddRows="False" x:Name="EGrid" Grid.Column="0" ItemsSource="{Binding Nums}" >
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Number">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Converter={StaticResource DecimalFormatConverter}, ConverterParameter='## ### ###.00'}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

输出:

【讨论】:

  • 那里我做了一个vb.net版本。
  • 首先,感谢您的意见。这似乎是要走的路。但是,在我的情况下,我无法手动定义每一列,因为它们可能会发生变化(数据是从在运行时随事件发生变化的请求中加载的)。我正在尝试使用可从 DataGrid 本身访问的另一个属性来解决它,但找不到它。
  • 我以编程方式更正了它,因为我意识到您需要列的实例来格式化其单元格模板。连接到 AutoGeneratingColumn 事件的事件处理程序使每个动态生成的列成为可能。你带我上路,那我就接受你的回答。谢谢!
  • 很高兴能帮上忙!
【解决方案2】:

您需要将其转换为字符串,将其拆分并在需要的地方添加空格。

我对您的问题进行了硬编码。 如果您希望 sn-p 使用不同的数字,我建议您使用相同的逻辑并在数字的长度上创建一个 Select/Case

这是解决您问题的代码:

    Dim number As Double = 1000000.52
    Dim str() As String = Convert.ToString(number).Split(",")
    str(0) = "" & _
        str(0).Substring(0, 1) & _
        " " & str(0).Substring(1, 3) & _
        " " & str(0).Substring(4, 3) & _
        "." & str(1)

【讨论】:

  • 如果我的回答能让你满意,我会很高兴accpet it
  • 你运行了这段代码吗?它会引发索引超出范围错误。
  • 我没有看到数字中的逗号,所以 split 函数返回一个单元素数组。 str(1) 将导致错误。
  • 不,很抱歉,但 1000000.52 以 1000000,52 的形式保存到双变量中
  • 感谢您的输入,但在可能有数千行和数十列的 DataGrid 中检测数字数据、对其进行转换然后对其进行格式化似乎是一项非常繁重的处理。
猜你喜欢
  • 1970-01-01
  • 2011-10-08
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 2018-04-29
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
相关资源
最近更新 更多