WPF DatePicker 显示时间和日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25764111/
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
WPF DatePicker to show the time as well as the date
提问by user3428422
I have a DatePicker
object in a DataGrid
that successfully shows the date from the database or a property:
我DatePicker
在 aDataGrid
中有一个对象,它成功地显示了来自数据库或属性的日期:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Date, StringFormat=dd/MM/yyyy}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Now I want to show the time in front of the date. This is what I tried, but it doesn't work:
现在我想在日期前面显示时间。这是我尝试过的,但不起作用:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Date, StringFormat='dd/MM/yyyy HH:mm:ss'}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
NOTE: I do notwant to do this in the code-behind.
注:我不希望这样做的代码隐藏。
采纳答案by zaman
Use the extended DateTimePicker
使用扩展的 DateTimePicker
回答by daniele3004
In addition to, without using other components, if you just want to display a datein the format that you want to edit it, you can do this:
此外,在不使用其他组件的情况下,如果您只想以您想要编辑的格式显示日期,您可以这样做:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Date,StringFormat={}{0:dd/MM/yyyy}}" VerticalAlignment="Center" HorizontalAlignment="Left">
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
回答by Lyra
You could do something like this:
你可以这样做:
<DataGridTemplateColumn Header="YourDate">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<DatePicker SelectedDate="{Binding YourDate, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Margin="2 0 0 0" Text="{Binding YourDate, UpdateSourceTrigger=PropertyChanged, StringFormat='t'}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding YourDate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
This will show an DatePicker next to an TextBox wich are bound to the same property. The TextBox got an StringFormat to only show the date.
这将在绑定到相同属性的 TextBox 旁边显示一个 DatePicker。TextBox 有一个 StringFormat 来只显示日期。
UpdateSourceTrigger is set to PropertyChanged, so the user can't do an conflict.
UpdateSourceTrigger 设置为 PropertyChanged,因此用户不能进行冲突。