使用 WPF 的日期转换器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18662870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:36:22  来源:igfitidea点击:

Date Converter using WPF

c#wpfwpf-controlswpfdatagrid

提问by Robert

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.

一旦从 DatePicker 中选择了日期,我就制作了这个转换器来获取当前时间。在字符串日期中,我获得了从 DatePicker 中选择的值,但我似乎不能只获得日期。进入 Value 属性的格式是 9/24/2013 12:00:00,但我希望它是 9/24/2013。我已经在datetime converter WPF 上问过一个类似的问题,但提供的答案都没有奏效。我收到同样的错误:指定的演员表无效。

回答by Nitin

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

您不需要转换器来执行此操作。您可以使用StringFormatin 绑定本身来格式化您选择的日期时间,以仅以 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; }

回答by Abhay Kalariya

Try this.

尝试这个。

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