C# 如何在 wpf 中使用 DateTimePicker 绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17609171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:01:01  来源:igfitidea点击:

How to use DateTimePicker binding in wpf

c#wpfxamlbindingdatepicker

提问by Dinesh

In my wpf application, in CustomView window, following are the properties which I declared,

在我的 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;

In xaml,

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

In this way, I'm getting Date in my starttime and stoptime controls. But I want time in "hh:mm tt" formats. DateTimePicker control is not available in WPF toolbox. so for getting time in the specified format instead of date, what should I do? Please suggest.

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

采纳答案by Chitta

try to use http://wpftoolkit.codeplex.com/documentation

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

Refer to the following namespace

参考以下命名空间

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

and then

进而

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