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
"WpfDateTimePicker.MainWindow"
        ...        
        xmlns:wpfx="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit">
    <Grid>
        
            "DateTimePicker1" Height="25" Width="250" Margin="5" ShowButtonSpinner="False" 
                                 Format="Custom" FormatString="yyyy年MM月dd日 HH:mm:ss" Text=" 年 月 日 : :"/>

            "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"/>

            "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"/>

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

            "DateTimePicker5" Height="25" Width="250" Margin="5" ShowButtonSpinner="False"
                                 Format="Custom" FormatString="yyyy/MM/dd" Text="2011/10/11"/>

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

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

            "DateTimePicker8" Height="25" Width="250" Margin="5" ShowButtonSpinner="False"
                                 Format="Custom" FormatString="yyyy/MM/dd" Text="2011/10/11"/>

        
    Grid>

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:30TextBox>

        <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>

你可能感兴趣的:(WPF DateTimePicker)