如何在wpf中使用DateTimePicker绑定

本文关键字:DateTimePicker 绑定 wpf | 更新日期: 2023-09-27 18:22:07

在我的wpf应用程序中,在CustomView窗口中,以下是我声明的属性,

private DateTime starttime 
    {
        get
        {
            return DateTime.Parse(StartTimeText.Text); 
        }
        set
        {
            StartTimeText.Text = value.ToString();
            OnPropertyChanged("starttime");
        } 
    }
    private DateTime stoptime
    {
        get
        {
            return DateTime.Parse(StopTimeText.Text);
        }
        set
        {
            StopTimeText.Text = value.ToString();
            OnPropertyChanged("stoptime");
        }
    }
protected virtual void OnPropertyChanged(String time)
    {
        if (System.String.IsNullOrEmpty(time))
        {
            return;
        }
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(time));
        }
    }
public event PropertyChangedEventHandler PropertyChanged;

在xaml中,

<DatePicker x:Name="StartTimeText" 
            SelectedDate="{Binding Path=starttime}"  
            BorderThickness="0" 
            Background="Yellow"
            Width="100"/>
<DatePicker x:Name="StopTimeText" 
            SelectedDate="{Binding Path=stoptime, Mode=TwoWay}" 
            BorderThickness="0" 
            Background="Yellow"
            Width="60"/>

通过这种方式,我在开始时间和停止时间控件中获得了Date。但是我想要"hh:mm-tt"格式的时间。DateTimePicker控件在WPF工具箱中不可用。那么,要以指定的格式获取时间而不是日期,我该怎么办?请提出建议。

如何在wpf中使用DateTimePicker绑定

尝试使用http://wpftoolkit.codeplex.com/documentation

请参阅以下命名空间

xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" 

然后

<xctk:DateTimePicker x:Name="dtpStartTime"  
                     Format="Custom" 
                     FormatString="HH:mm tt" 
                     Margin="5"/>