munchine
munchine

Reputation: 499

XAML datepicker to only return date but not time

I wanted to return just the date (not the time), but I have tried everything and it always returns

7/23/2013 12:00:00 AM

but I only want

7/23/2013

Here's my code, and it needs to be done in pure XAML.

            <DatePicker SelectedDate="{Binding myDateProperty}" Height="30" HorizontalAlignment="Left" Margin="0,1,5,1" Name="ObsoleteDate" VerticalAlignment="Top" Width="110">
           <DatePicker.Resources>
              <Style TargetType="{x:Type DatePickerTextBox}">
                 <Setter Property="Control.Template">
                    <Setter.Value>
                       <ControlTemplate>
                          <TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat={}{0:dd/MM/yyyy}}"/>
                       </ControlTemplate>
                    </Setter.Value>
                 </Setter>
              </Style>
           </DatePicker.Resources>
        </DatePicker>

Upvotes: 1

Views: 4442

Answers (3)

Amal
Amal

Reputation: 461

<TextBox Background="#fff" x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, 
RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat=d}" />

Worked for me

Upvotes: 2

Sabyasachi Mishra
Sabyasachi Mishra

Reputation: 1749

I used the below code and it worked for me.(ObsoleteDate is the name of your DatePicker)

ObsoleteDate.SelectedDate.Value.Date.ToShortDateString()

Upvotes: 0

Arushi Agrawal
Arushi Agrawal

Reputation: 629

Use StringFormat=d in place of StringFormat={}{0:dd/MM/yyyy}}

Upvotes: 1

Related Questions