WPF DateTimePicker

WPF没有DateTimePicker,自身的DatePicker不能选择时间。所以要自己实现。

<DatePicker Height="25" Width="250" Text="2011/10/11 15:16:17"/>
  1. 使用WinForm的DateTimePicker
<Window x:Class="WinFormDateTimePicker.MainWindow"
...
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">
    <Grid>
        <StackPanel>
            <wfi:WindowsFormsHost  Width="190"  Margin="5">
                <wf:DateTimePicker x:Name="dtp1" Format="Custom" CustomFormat="yyyy年MM月dd日 HH:mm:ss" ShowUpDown="False" Text="2011/10/11 9:10:11"/>
            </wfi:WindowsFormsHost>
            <wfi:WindowsFormsHost  Width="190"  Margin="5">
                <wf:DateTimePicker x:Name="dtp2" Format="Custom" CustomFormat="yyyy/MM/dd hh:mm:ss" ShowUpDown="False" Text="2011/10/11 9:10:11"/>
            </wfi:WindowsFormsHost>
            <wfi:WindowsFormsHost  Width="190"  Margin="5">
                <wf:DateTimePicker x:Name="dtp3" Format="Custom" CustomFormat="yyyy-MM-dd hh:mm:ss" ShowUpDown="False" Text="2011/10/11 9:10:11"/>
            </wfi:WindowsFormsHost>
            <wfi:WindowsFormsHost  Width="190"  Margin="5">
                <wf:DateTimePicker x:Name="dtp4" Format="Custom" CustomFormat="yyyy年MM月dd日" ShowUpDown="False" Text="2011/10/11 9:10:11"/>
            </wfi:WindowsFormsHost>
            <wfi:WindowsFormsHost  Width="190"  Margin="5">
                <wf:DateTimePicker x:Name="dtp5" Format="Custom" CustomFormat="yyy/MM/dd" ShowUpDown="False" Text="2011/10/11 9:10:11"/>
            </wfi:WindowsFormsHost>
            <wfi:WindowsFormsHost  Width="190"  Margin="5">
                <wf:DateTimePicker x:Name="dtp6" Format="Custom" CustomFormat="yyyy-MM-dd" ShowUpDown="False" Text="2011/10/11 9:10:11"/>
            </wfi:WindowsFormsHost>
            <wfi:WindowsFormsHost  Width="190"  Margin="5">
                <wf:DateTimePicker x:Name="dtp7" Format="Custom" CustomFormat="HH:mm:ss" ShowUpDown="False" Text="2011/10/11 9:10:11"/>
            </wfi:WindowsFormsHost>
        </StackPanel>
    </Grid>
</Window>
  1. 使用Wpf.Toolkit
<Window x:Class="WpfDateTimePicker.MainWindow"
        ...        
        xmlns:wpfx="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit">
    <Grid>
        <StackPanel>
            <wpfx:DateTimePicker x:Name="DateTimePicker1" Height="25" Width="250" Margin="5" ShowButtonSpinner="False" 
                                 Format="Custom" FormatString="yyyy年MM月dd日 HH:mm:ss" Text=" 年 月 日 : :"/>

            <wpfx:DateTimePicker x:Name="DateTimePicker2" Height="25" Width="250" Margin="5" ShowButtonSpinner="False"
                                 Format="Custom" FormatString="yyyy/MM/dd hh:mm:ss" Text="2011/10/11 15:16:17"/>

            <wpfx:DateTimePicker x:Name="DateTimePicker3" Height="25" Width="250" Margin="5" ShowButtonSpinner="False"
                                 Format="Custom" FormatString="yyyy-MM-dd hh:mm:ss" Text="2011/10/11 15:16:17"/>

            <wpfx:DateTimePicker x:Name="DateTimePicker4" Height="25" Width="250" Margin="5" ShowButtonSpinner="False"
                                 Format="Custom" FormatString="yyyy年MM月dd日" Text="2011年10月11日"/>

            <wpfx:DateTimePicker x:Name="DateTimePicker5" Height="25" Width="250" Margin="5" ShowButtonSpinner="False"
                                 Format="Custom" FormatString="yyyy/MM/dd" Text="2011/10/11"/>

            <wpfx:DateTimePicker x:Name="DateTimePicker6" Height="25" Width="250" Margin="5" ShowButtonSpinner="False"
                                 Format="Custom" FormatString="yyyy-MM-dd" Text="2011/10/11"/>

            <wpfx:DateTimePicker x:Name="DateTimePicker7" Height="25" Width="250" Margin="5" ShowButtonSpinner="False"
                                 Format="Custom" FormatString="HH:mm:ss" Text="15:16:17"/>

            <wpfx:DateTimePicker x:Name="DateTimePicker8" Height="25" Width="250" Margin="5" ShowButtonSpinner="False"
                                 Format="Custom" FormatString="yyyy/MM/dd" Text="2011/10/11"/>

        </StackPanel>
    </Grid>
</Window>

3.UserControl

<UserControl x:Class="MyDTPicker.DateTimePicker"
             ...
             xmlns:local="clr-namespace:MyDTPicker"
             mc:Ignorable="d">

    <StackPanel Orientation="Horizontal">
        <TextBox x:Name="DateDisplay" 
                     VerticalContentAlignment="Center" 
                     Margin="0,0,0,0" 
                     MinHeight="{Binding ElementName=PopUpCalendarButton, Path=ActualHeight}" >2017-10-23 12:30</TextBox>

        <ToggleButton Background="{Binding Path=Background}"
                      MaxHeight="21" 
                      Margin="-1,0,0,0" 
                      Name="PopUpCalendarButton" 
                      IsChecked="False">
            <Grid x:Name="arrowGlyph" Grid.Column="1" IsHitTestVisible="False" Margin="5">
                <Path x:Name="Arrow" Data="M0,0L3,0 4.5,1.5 6,0 9,0 4.5,4.5z" 
                      Fill="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" 
                      Height="5" Margin="0,1,0,0" Width="9"/>
            </Grid>
        </ToggleButton>

        <Popup IsOpen="{Binding Path=IsChecked, ElementName=PopUpCalendarButton}" 
               x:Name="CalendarPopup" 
               Margin="0,-7,0,0"
               PopupAnimation="Fade"
               StaysOpen="False">
            <Calendar Margin="0,-1,0,0" x:Name="CalDisplay"/>
        </Popup>
    </StackPanel>

</UserControl>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值