使用WPF日期转换器 [英] Date Converter using WPF

查看:150
本文介绍了使用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
}



我有这样的转换器,我进行一次的日期是从DataPicker选择来获得当前的时间。在字符串日期我得到的是从选定的DatePicker的价值,但我不能似乎只获取日期。即进入Value属性的fromate是2013年9月24日十二时00分00秒,我想它是2013年9月24日。我已经要求在日期时间转换器WPF 问题simmlair,但在这里提供给我的答案非工作。我得到了同样的错误指定的转换是无效的。

I have this converter that i made to get the current time once the date is selected from the DataPicker. In string Date i am getting the value that was selected from the DatePicker, but i cant seem to only get the date. The fromate that is coming into the Value property is 9/24/2013 12:00:00 I would like it to be 9/24/2013. i have already asked a question simmlair at datetime converter WPF, but non of the answer that where provided to me worked. i get the same error Specified cast is not valid.

推荐答案

您不需要做这样的转换器。您可以使用的StringFormat 在结合自身格式化您选择的日期时间只显示为mm / dd / yyyy格式的日期。

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}}" />



我与此代码测试,它工作正常。
XAML:

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>

模型:

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

    }



视图模型具有TESTDATA的名单绑定到DataGrid的:

ViewModel has list of TestData to be bound to DataGrid:

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

这篇关于使用WPF日期转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆