Need to format dates in dynamically built WPF DataGrid

后端 未结 8 580
眼角桃花
眼角桃花 2020-12-05 09:51

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

相关标签:
8条回答
  • 2020-12-05 10:45

    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.

    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>
    
    0 讨论(0)
  • 2020-12-05 10:45

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

    worked beautifuly

    0 讨论(0)
提交回复
热议问题