简体   繁体   English

使用WPF的日期转换器

[英]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. 从DatePicker中选择日期后,我将此转换器设置为获取当前时间。 In string Date I get the value that was selected from the DatePicker, but I can't seem to only get the date. 在字符串日期我得到从DatePicker中选择的值,但我似乎只能得到日期。 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. 进入Value属性的格式是9/24/2013 12:00:00,但我希望它是9/24/2013。 I have already asked a similar question at datetime converter WPF , but none of the provided answers worked. 我已经在datetime转换器WPF上问了一个类似的问题,但没有提供的答案有效。 I get the same error: Specified cast is not valid. 我得到了同样的错误:指定的强制转换无效。

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. 您可以使用StringFormat绑定自身来格式化您选择的日期时间,以mm / dd / yyyy格式显示日期。

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

I tested with this code and it is working fine. 我测试了这段代码,它工作正常。 XAML: 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: ViewModel具有要绑定到DataGrid的TestData列表:

public List<TestData> TestList { get; set; }

Try this. 尝试这个。

DateTime test = (DateTime) value ;
string date = test.Date.ToString("d/M/yyyy"); 
//Use test.Date.tostring instad of test.tostring(...)
return (date);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM