7
public class DateTimeConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values != null)
        {
            DateTime test = (DateTime) value ;
            string date = test.ToString("d/M/yyyy");
            return (date);
        }
        return string.Empty;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

I made this converter to get the current time once the date is selected from the DatePicker. In string Date I get the value that was selected from the DatePicker, but I can't seem to only get the date. The format that is coming into the Value property is 9/24/2013 12:00:00, but I would like it to be 9/24/2013. I have already asked a similar question at datetime converter WPF, but none of the provided answers worked. I get the same error: Specified cast is not valid.

Robert
  • 646
  • 4
  • 12
  • 37
  • Why do you even have this class? DateTimes have tons of built-in .ToString conversions. If that's all this is doing--there's no point in having this. – Jon La Marr Sep 06 '13 at 17:02
  • i am calling it from my XAML I thought that i need to do it from a Converter. If i dont can you please show an example – Robert Sep 06 '13 at 17:05
  • and i would also like to to add the correct time – Robert Sep 06 '13 at 17:26

2 Answers2

19

You dont need a converter for doing this. You can use the StringFormat in binding itself to format your selected datetime to show just date in mm/dd/yyyy format.

<TextBlock Text="{Binding Date, StringFormat={}{0:MM/dd/yyyy}}" />

I tested with this code and it is working fine. XAML:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding TestList}">
            <DataGrid.Columns>
            <DataGridTemplateColumn Header="Start">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Start, StringFormat=d}" FontFamily="Verdana" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <DatePicker SelectedDate="{Binding Start}" FontFamily="Verdana"  >
                        <DatePicker.CalendarStyle>
                            <Style TargetType="Calendar">
                                <Setter Property="DisplayMode" Value="Month"/>
                            </Style>
                        </DatePicker.CalendarStyle>
                    </DatePicker>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

Model:

    public class TestData
    {
        DateTime start;
        public DateTime Start
        {
            get { return start; }
            set { start = value; }
        }

    }

ViewModel has list of TestData to be bound to DataGrid:

public List<TestData> TestList { get; set; }
Nitin
  • 18,344
  • 2
  • 36
  • 53
  • that is what i was trying but right when the textblock lost focus it goes back to adding in the 12:00:00 AM. I have also looked in the lost the focus to see if that is doing anything, but it is not – Robert Sep 06 '13 at 17:28
  • can you share your xaml and vm code?.. it should work ..i need to check your code to help u.. – Nitin Sep 06 '13 at 17:38
  • here is the like to my xaml code http://stackoverflow.com/questions/18643393/wpf-date-string-format-in-xaml – Robert Sep 06 '13 at 17:46
  • please check if the updated answer code working fine at your end? – Nitin Sep 06 '13 at 18:02
  • when i changed the binding names from start to start1 the string format worked. I am thinking it has something to do with the data being bind from the data source – Robert Sep 06 '13 at 18:08
  • can the data from the ds mess with the binding when trying to change it? – Robert Sep 06 '13 at 18:09
0

Try this.

DateTime test = (DateTime) value ;
string date = test.Date.ToString("d/M/yyyy"); 
//Use test.Date.tostring instad of test.tostring(...)
return (date);
slfan
  • 8,950
  • 115
  • 65
  • 78