wpf 如何使用属性更改 TimePicker 格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17739768/
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-13 09:14:29  来源:igfitidea点击:

How to change TimePicker format using properties

c#wpftimepicker

提问by Dinesh

I've used TimePicker format in my wpf application using extended wpf toolkit. I'm using "HH:MM" format. but problem is, the button which allows to select hours directly from the options, showing 24 hours format, but I want 12 hours only. But I managed that through properties of TimePicker by changing Endtime from "23:59" to "11:59". But those up and down buttons still showing hours upto 23. so, If I want to make upto 11, what should I do? Can I achieve this by TimePicker's property only? also It's not showing part of day i.e AM/PM. I also want another button which should show me these two options (i.e AM and PM). Please suggest, how should I proceed?

我使用扩展的 wpf 工具包在我的 wpf 应用程序中使用了 TimePicker 格式。我正在使用“HH:MM”格式。但问题是,允许直接从选项中选择小时的按钮,显示 24 小时格式,但我只想要 12 小时。但是我通过将 Endtime 从“23:59”更改为“11:59”,通过 TimePicker 的属性实现了这一点。但是那些向上和向下按钮仍然显示小时到 23 点。所以,如果我想达到 11 点,我该怎么办?我可以仅通过 TimePicker 的属性来实现吗?它也没有显示一天的一部分,即上午/下午。我还想要另一个按钮来显示这两个选项(即 AM 和 PM)。请建议,我应该如何进行?

My Timepicker controls which are bound to DateTime object in my code behind are as follows,

我的 Timepicker 控件在我后面的代码中绑定到 DateTime 对象如下,

<Window x:Class="TimeSheet.CustomView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
    x:Name="Time"
    Title="CustomView" Width="596" Height="596">
<Grid>
    <StackPanel Orientation="Horizontal" Height="30" Width="596" Margin="0,137,-8,399">
        <xctk:TimePicker x:Name="StartTimeText" Value="{Binding starttime, ElementName=Time, Mode=TwoWay}" BorderThickness="0" Background="Yellow" Width="113" EndTime="11:59:0" AllowSpin="False"/>
        <xctk:TimePicker x:Name="StopTimeText" Value="{Binding stoptime, ElementName=Time, Mode=TwoWay}" BorderThickness="0" Background="Yellow" Width="115" EndTime="11:59:0" AllowSpin="False"/>
    </StackPanel>              
</Grid>
</Window>

UI of my Window-

我的窗口的用户界面-

enter image description here

在此处输入图片说明

回答by Vanlalhriata

Use the FormatStringproperty to format your display. I think you have the formats covered in your previous questions. You should also set Formatto Custom.

使用该FormatString属性来格式化您的显示。我认为您已经在之前的问题中涵盖了格式。您还应该设置FormatCustom.

Here's a suggestion:

这是一个建议:

<xctk:TimePicker Format="Custom" FormatString="hh:mm:ss tt" />

You should note that using HHfor hours gives you 24-hour format, while hhwill give you a 12-hour format.

您应该注意,使用HHfor hours 为您提供 24 小时格式,而hh将为您提供 12 小时格式。

You can check the documentations here.

您可以在此处查看文档。