加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在wpf中使用DateTimePicker绑定

发布时间:2020-12-15 04:14:54 所属栏目:百科 来源:网络整理
导读:在我的wpf应用程序中,在CustomView窗口中,以下是我声明的属性, private DateTime starttime { get { return DateTime.Parse(StartTimeText.Text); } set { StartTimeText.Text = value.ToString(); OnPropertyChanged("starttime"); } } private DateTime st
在我的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"/>

通过这种方式,我在启动时和停止时间控件中获取日期.但我希望时间用“hh:mm tt”格式. 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"/>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读