【问题标题】:How to use DateTimePicker binding in wpf如何在 wpf 中使用 DateTimePicker 绑定
【发布时间】:2013-07-10 15:46:20
【问题描述】:

在我的 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”格式的时间。 DateTimePicker 控件在 WPF 工具箱中不可用。所以要以指定格式而不是日期获取时间,我该怎么办?请提出建议。

【问题讨论】:

  • 你不是问过类似的问题here吗?
  • 没有。那是不同的。它与组合框绑定有关。现在想使用 TimePicker 而不是组合框。
  • 我明白了。无论如何,我注意到您还没有接受答案there。在我发布我的之前有一个答案。您正在寻找扩展的 WPF 工具包

标签: c# wpf xaml binding datepicker


【解决方案1】:

尝试使用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"/>

【讨论】:

  • 是的。我这样做了。太感谢了。我得到了预期的结果。
猜你喜欢
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多