Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use DateTimePicker binding in wpf

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

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,

<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.

like image 411
Dinesh Avatar asked Jul 12 '13 06:07

Dinesh


1 Answers

try to use 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"/>
like image 199
Chitta Avatar answered Sep 20 '22 00:09

Chitta