在wpf datagrid中编辑日期时,如何更改日期格式

发布于 2025-01-23 05:17:01 字数 761 浏览 2 评论 0原文

每当我更改WPF DataGrid中的日期时,它都会尝试将我的输入转换为MM-DD-yyyy。

XAML

    <Grid>
        <DataGrid x:Name="datagrid" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="DGC_CreatedTime" Header="Created" Binding="{Binding CreatedTime, StringFormat=dd/MM/yyyy}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

以正确的格式显示数据时,DD/mm/yyyy,但是当它尝试更改为04/08/2022之类的内容时,它将其显示为08/04/2022。

而且,如果我输入25/08/2022,则不接受日期,因为没有第25个月。

我已经检查了语言 /区域设置,可以确认它们的格式正确。 我也尝试添加... system.threading.thread.currentthread.currentculture = new CultureInfo(“ en-nz”);

我可以添加CellEditEding事件并将输入的值更改为正确的格式,但是似乎应该有一个更简单的方法。

Whenever I change a date in my WPF datagrid, it try's to convert my input to MM-dd-yyyy.

XAML

    <Grid>
        <DataGrid x:Name="datagrid" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="DGC_CreatedTime" Header="Created" Binding="{Binding CreatedTime, StringFormat=dd/MM/yyyy}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

When the data is displayed in is in the correct format, dd/MM/yyyy, however when it try to change to something like 04/08/2022 then it displays it as 08/04/2022.

And if I type in 25/08/2022, then it does not accept the date, as there is no 25th month.

I have checked the language / region settings and can confirm they are in the right format.
I have also tried adding...
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-NZ");

I can add the CellEditEnding event and change the entered value to the correct format, but it seems there should be a much simpler way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み青杉依旧 2025-01-30 05:17:01

尝试设置绑定的convertrultur属性:

<DataGridTextColumn x:Name="DGC_CreatedTime" Header="Created"
                    Binding="{Binding CreatedTime, StringFormat=dd/MM/yyyy, 
                        ConverterCulture=en-NZ}"/>

Try to set the ConverterCulture property of the binding:

<DataGridTextColumn x:Name="DGC_CreatedTime" Header="Created"
                    Binding="{Binding CreatedTime, StringFormat=dd/MM/yyyy, 
                        ConverterCulture=en-NZ}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文