简体   繁体   中英

How do I format a double value in XAML to German format on a DataGridTextColumn?

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}"/>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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