一起使用silverlight datepicker和timepicker [英] Using silverlight datepicker and timepicker together

查看:129
本文介绍了一起使用silverlight datepicker和timepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用工具包中的Silverlight DatePicker和TimePicker来允许用户选择日期和时间。它们在与Scences背后的业务对象上绑定相同的DateTime值。这主要工作正常,除了当日期组件被更改时间组件被擦除。这是有道理的,但可能不是用户想要的行为。

I'm using the Silverlight DatePicker and TimePicker from the toolkit together to allow a user to select a date and time. They are bound to the same DateTime value on a business object behind the scences. This mostly works okay, except when the date component is changed the time component is wiped. This is kinda logical but probably not the behaviour the user wanted.

有几种方法可以攻破这个问题:

There's a couple of ways I could hack my way round this:


  • 将日期和时间组件存储在不同的值(不是那么简单但有点麻烦,因为我要将结果值作为一个字段存储在数据库中)

  • 当SelectedDateChanged事件触发时,尝试修复时间组件(这似乎是一个非常棘手的解决方案)

我想要告诉DatePicker控件:只要在更改日期时离开时间组件。我希望太多吗?

I'd like to be able to tell the DatePicker control: just leave the time component when you change date. Am I hoping for too much?

推荐答案

我想,你可以说DatePicker只要离开时间组件,当你更改日期使用转换器:)。当你绑定DateTime到DatePicker转换器存储值,并在ConvertBack上返回没有时间部分的更改。

I think, you can say DatePicker "Just leave the time component when you change date" using converter :). When you are binding DateTime to DatePicker converter stores value, and on ConvertBack returns without changes in Time part.

public class DateConverter : IValueConverter
{
    private DateTime _original;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        _original = (DateTime)value;
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime date = ((DateTime) value);
        return new DateTime(date.Year, date.Month, date.Day, _original.Hour, _original.Minute, _original.Second);
    }
}

XAML:

<sdk:DatePicker SelectedDate="{Binding Date, Mode=TwoWay, 
                Converter={StaticResource dateConverter}}" />

<input:TimePicker Value="{Binding Date, Mode=TwoWay}"/>

这篇关于一起使用silverlight datepicker和timepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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