How do I format a double value in XAML to German f

2019-08-02 05:47发布

问题:

I have this simple piece of code and I am wondering how to convert it to German format (#.###,##) ?

// displays #.##
<DataGridTextColumn Header="Sum Value" Binding="{Binding SumValue}"/>

回答1:

In this case try several options:

A. Global setting language culture for the entire application, it can solve some problems related to the current language culture.

Add Startup event for App.xaml, which establishes the necessary language culture. It affects the display of the date, double values​​, sums of money, etc:

XAML

<Application x:Class="DataGridTargetUpdateHelp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             Startup="Application_Startup">

    <Application.Resources>

    </Application.Resources>
</Application>

Code-behind

public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
            new FrameworkPropertyMetadata(System.Windows.Markup.XmlLanguage.GetLanguage("de-DE"))); // CultureInfo.CurrentCulture.IetfLanguageTag return the current code of language
    }
}

B. Use the DataGridTemplateColumn

In this case you can set any Control to display values in the DataGridCell:

<DataGrid.Columns>
    <DataGridTemplateColumn Header="ID">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=ID, StringFormat={}{0:N2}}" xml:lang="de-DE" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>