【问题标题】:XAML datepicker to only return date but not timeXAML 日期选择器只返回日期而不是时间
【发布时间】:2013-07-02 05:42:57
【问题描述】:

我只想返回日期(而不是时间),但我已经尝试了所有方法,它总是返回

7/23/2013 12:00:00 AM

但我只想要

7/23/2013

这是我的代码,需要在纯 XAML 中完成。

            <DatePicker SelectedDate="{Binding myDateProperty}" Height="30" HorizontalAlignment="Left" Margin="0,1,5,1" Name="ObsoleteDate" VerticalAlignment="Top" Width="110">
           <DatePicker.Resources>
              <Style TargetType="{x:Type DatePickerTextBox}">
                 <Setter Property="Control.Template">
                    <Setter.Value>
                       <ControlTemplate>
                          <TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat={}{0:dd/MM/yyyy}}"/>
                       </ControlTemplate>
                    </Setter.Value>
                 </Setter>
              </Style>
           </DatePicker.Resources>
        </DatePicker>

【问题讨论】:

  • 您只需要日期吗?在用户界面上还是在您的视图模型中?你试过 SelectedDateFormat 了吗?
  • 在视图模型中。试过 SelectedDateFormat="Short" 但结果相同。
  • "12:00:00" 是默认值,如果您不关心时间,可以使用 Date 部分:msdn.microsoft.com/en-us/library/system.datetime.date.aspx
  • 你能显示你的视图模型代码吗?你总是可以使用转换器来得到你想要的

标签: wpf xaml datepicker


【解决方案1】:

使用StringFormat=d 代替StringFormat={}{0:dd/MM/yyyy}}

【讨论】:

    【解决方案2】:
    <TextBox Background="#fff" x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, 
    RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat=d}" />
    

    为我工作

    【讨论】:

      【解决方案3】:

      我使用了下面的代码,它对我有用。(ObsoleteDate 是您的 DatePicker 的名称)

      ObsoleteDate.SelectedDate.Value.Date.ToShortDateString()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-07
        • 1970-01-01
        • 1970-01-01
        • 2012-09-11
        • 2020-02-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多