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

本秂侑毒 提交于 2020-01-06 02:18:45

问题


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>


来源:https://stackoverflow.com/questions/23325627/how-do-i-format-a-double-value-in-xaml-to-german-format-on-a-datagridtextcolumn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!