格式化Datagrid中的值

时间:2010-11-28 21:56:02

标签: c# wpf

有没有办法格式化绑定到数据网格的值?例如,我有以下内容:

<DataGrid AutoGenerateColumns="False" Height="487" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dgTransactionLog" VerticalAlignment="Top" Width="404">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=Date}" Header="Date" />
        <DataGridTextColumn Binding="{Binding Path=Payee1.Name}" Header="To/From" />
        <DataGridTextColumn Binding="{Binding Path=Amount}" Header="Amount" />
    </DataGrid.Columns>
</DataGrid>

我希望Date列只是日期(不是时间),Amount列是货币格式。以下是我填充数据网格的方法:

var transactions = TransactionManager.GetTransactions();
dgTransactionLog.ItemsSource = transactions;

3 个答案:

答案 0 :(得分:20)

使用StringFormat属性:

<DataGridTextColumn Binding="{Binding Path=Date, StringFormat=d}" Header="Date" />
<DataGridTextColumn Binding="{Binding Path=Amount, StringFormat=C}" Header="Amount" />

答案 1 :(得分:4)

一种最简单的方法。这里在下面的代码中使用您的语言代码作为ConverterCulture的值。 你可以找到你的语言code here

package com.qualcomm.ftcrobotcontroller.opmodes;

public class BasicFunctions extends HardwareAccess {

    /*
    This file lists some basic funcions on the robot that we can use without typing large amounts of code.
    To use this code in the opmode add "extends BasicFunctions" to the end of the class line
     */
    public void setDriveMotorPower(double power1, double power2) {
        if (power2 = null) {
            motorLeft.setPower(power1);
            motorRight.setPower(power1);
        }

        motorLeft.setPower(power1);
        motorRight.setPower(power2);

    }

    public void init() {

    }

    @Override
    public void loop() {

    }
}

输出将使用您的本地货币output screenshot

除货币以外的任何内容查找stringFormat说明符here

答案 2 :(得分:0)

如果stringformat是可变的,则上述解决方案将不起作用。

<DataGrid.Resources>
    <DataTemplate x:Key="YTemplate">
        <Label Content="{Binding Y, Mode=OneWay}" ContentStringFormat="{Binding StringFormat, ElementName=Parent_UC}"/>
    </DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
    <DataGridTemplateColumn Header="Y" Width="*" CellTemplate="{StaticResource YTemplate}"/>
</DataGrid.Columns>