在WPF中使用DateTimePicker,可以使用第三方库或自定义控件。以下是一些常见的方法:
推荐的第三方库有:Xceed.Wpf.Toolkit和MaterialDesignThemes。
Xceed.Wpf.Toolkit是一个免费的库,其中包含了DateTimePicker控件。可以通过NuGet包管理器安装:
Install-Package Xceed.Wpf.Toolkit
在XAML中使用DateTimePicker:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
Title="MainWindow" Height="450" Width="800">
<Grid>
<xctk:DateTimePicker HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="200"/>
</Grid>
</Window>
MaterialDesignThemes是一个免费的库,其中包含了DateTimePicker控件。可以通过NuGet包管理器安装:
Install-Package MaterialDesignThemes
在XAML中使用DateTimePicker:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
Title="MainWindow" Height="450" Width="800">
<Grid>
<materialDesign:DateTimePicker HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="200"/>
</Grid>
</Window>
可以通过自定义控件的方式实现DateTimePicker。以下是一个简单的示例:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="200">
<DatePicker x:Name="datePicker" DisplayDate="{Binding ElementName=dateTimePicker, Path=SelectedDate}" />
<TimePicker x:Name="timePicker" SelectedTime="{Binding ElementName=dateTimePicker, Path=SelectedTime}" />
</Grid>
</Grid>
</Window>
在这个示例中,我们使用了两个控件:DatePicker和TimePicker。通过绑定SelectedDate和SelectedTime属性,我们可以实现一个简单的DateTimePicker控件。
推荐的腾讯云相关产品: