【问题标题】:WPF GridView: Changing the DateTime formatting for bound DataTableWPF GridView:更改绑定 DataTable 的 DateTime 格式
【发布时间】:2009-06-26 11:56:28
【问题描述】:

我们正在使用动态绑定到 DataTable 的 WPF GridView(这又是 DB 查询的直接结果)。这基本上可以正常工作,并将数据库查询的任何结果显示在一个漂亮的表中。

现在的问题是某些结果包含 DateTime 列,并且显示的日期始终是美国格式,而不是用户指定的日期格式。

有没有简单的方法来设置 GridView 显示的日期时间格式?

我们事先不知道哪些列将是 DateTime,因为显示了许多不同的表。

我们填充gridview的方式如下:

        MyListView.DataContext = data;
        MyListView.SetBinding(ListView.ItemsSourceProperty, new Binding());
        MyGridView.Columns.Clear();

        foreach (DataColumn col in data.Columns)
        {
            GridViewColumn gvcol = new GridViewColumn();
            gvcol.Header = col.ColumnName;
            gvcol.DisplayMemberBinding = new Binding(col.ColumnName);
            MyGridView.Columns.Add(gvcol);
        }

【问题讨论】:

    标签: wpf datetime gridview format


    【解决方案1】:

    将当前文化放在应用程序启动中就足够了

    /// <summary>
        /// Interaction logic for App.xaml
        /// </summary>
        public partial class App : Application
        {
            public App()
            {
                FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
    
            }
        }
    

    【讨论】:

    • 感谢您的快速回复,这行得通!现在还有一种方法可以稍后在应用程序中设置它,在显示登录屏幕之后,然后根据登录后返回的每个用户存储的语言而不是系统语言来设置它?
    • 除了上述解决方案:是否有专门为 GridView 更改日期格式的解决方案?
    猜你喜欢
    • 2011-04-07
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多