需要在动态构建的 WPF DataGrid 中格式化日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/848702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:25:01  来源:igfitidea点击:

Need to format dates in dynamically built WPF DataGrid

wpfdatetimedatagridformat

提问by FarrEver

We are binding an unknown result set to a WPF DataGrid at run time. Some of our columns are going to contain DateTime values and we need to properly format these date time fields. Without knowing which columns are going to be DateTime fields at design time, how are we able to format the columns at runtime?

我们在运行时将未知结果集绑定到 WPF DataGrid。我们的一些列将包含 DateTime 值,我们需要正确格式化这些日期时间字段。在设计时不知道哪些列将成为 DateTime 字段,我们如何在运行时格式化这些列?

We are using a DataTable's DefaultView to bind to the WPF DataGrid.

我们使用 DataTable 的 DefaultView 绑定到 WPF DataGrid。

回答by Junior Mayhé

Format the binding by StringFormat:

通过StringFormat以下方式格式化绑定:

<DataGridTextColumn Header="Fecha Entrada" 
                    Width="110"  
                    Binding="{Binding EnterDate, StringFormat={}\{0:dd/MM/yyyy hh:mm\}}"
                    IsReadOnly="True" />

I think it's better than writing code behind pieces of code

我认为这比在代码段后面写代码要好

回答by FarrEver

I figured out how to do this in code...hopefully there is a way to mimic this in XAML. (Please post if you find a working XAML sample.)

我想出了如何在代码中做到这一点......希望有一种方法可以在 XAML 中模仿这一点。(如果您找到有效的 XAML 示例,请发布。)

To accomplish this in code, add an event handler for the Grid's AutoGeneratingColumn event, such as:

要在代码中完成此操作,请为 Grid 的 AutoGeneratingColumn 事件添加一个事件处理程序,例如:

private void ResultsDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyType == typeof(DateTime))
    {
        DataGridTextColumn dataGridTextColumn = e.Column as DataGridTextColumn;
        if (dataGridTextColumn != null)
        {
            dataGridTextColumn.Binding.StringFormat = "{0:d}";
        }
    }
}

回答by Libi James

Hey you can set the locale culture info in the constructor of the WPF form as

嘿,您可以在 WPF 表单的构造函数中将区域设置文化信息设置为

this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);

this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);

Oryou can include the xml markup xml:lang="en-GB"in the window header markup

或者您可以在窗口标题标记中包含 xml 标记xml:lang="en-GB"

回答by MAXE

<DataGridTextColumn Header="Last update"
    Width="110"
    IsReadOnly="True"
    Binding="{Binding Path=Contact.TimeUpdate, StringFormat={}\{0:dd/MM/yyyy hh:mm\}, Mode=OneWay}" />

回答by Bryan Anderson

I would use a DataTemplate with a DataType of Date or DateTime (depending on which it will come through as). Place a TextBlock in the DataTemplate with a StringFormat in the binding.

我将使用 DataTemplate 与 Date 或 DateTime 的数据类型(取决于它将通过哪个)。在绑定中使用 StringFormat 在 DataTemplate 中放置一个 TextBlock。

Something like this should work (untested)

像这样的东西应该可以工作(未经测试)

<DataTemplate DataType="{x:Type DateTime}">
    <TextBlock Text="{Binding StringFormat={0:d}}"  />
</DataTemplate>

Or if you want it to apply just in the Grid

或者,如果您希望它仅在网格中应用

<wpfToolkit:DataGrid>
    <wpfToolkit:DataGrid.Resources>
        <DataTemplate DataType="{x:Type DateTime}">
            <TextBlock Text="{Binding StringFormat={0:d}}"  />
        </DataTemplate>
    </wpfToolkit:DataGrid.Resources>
    ...
</wpfToolkit:DataGrid>

回答by Web Developer

dataGridTextColumn.Binding.StringFormat = "{0:dd/MM/yyyy}";

dataGridTextColumn.Binding.StringFormat = "{0:dd/MM/yyyy}";

worked beautifuly

工作得很漂亮

回答by Rashed DIP

i run this way. its work complete .

我这样跑。它的工作完成了。

<TextBlock  Text="{Binding Date_start,  StringFormat=\{0:dd-MM-yyyy\}, Mode=OneWay}" />

回答by Rashed DIP

The answer of FarrEver, May 11 is good, but by me it doens't function. i still get American mm/dd/yyy instead my German dd/mm/yyyy. So I propose to find the regional settings of computer and use it in StringFormat

FarrEver,5 月 11 日的答案很好,但我认为它不起作用。我仍然得到美国的 mm/dd/yyy 而不是我的德国 dd/mm/yyyy。所以我建议找到计算机的区域设置并在StringFormat中使用

    Private Sub DGrid_AutoGeneratingColumn(ByVal sender As System.Object, ByVal e As Microsoft.Windows.Controls.DataGridAutoGeneratingColumnEventArgs)
    If e.PropertyType Is GetType(DateTime) Then
        Dim dataGridTextColumn As DataGridTextColumn = TryCast(e.Column, DataGridTextColumn)
        If dataGridTextColumn IsNot Nothing Then
            Dim ShortDatePattern As String = System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern
            dataGridTextColumn.Binding.StringFormat = "{0:" + ShortDatePattern + "}" '"{0:dd/MM/yyyy}"
        End If
    End If
End Sub

see also: my blog

另见:我的博客