Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 views
in Technique[技术] by (71.8m points)

c# - Date Converter using WPF

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...