仅限绑定日期和仅限时间

时间:2017-01-31 11:02:53

标签: c# wpf mvvm data-binding datepicker

我有2个控件:

DatePicker:仅适用于日期
带有TimeMask的TextBox:仅限时间

它们都链接到同一个属性DateTime EffectiveDate 但问题是当我更改DatePicker上的Date时,它会将TimeTextBox中的Time更改回12:00。

我理解它的原因,但是最好的解决方案是让它们分开工作但是绑定到相同的属性?

我试图占用当前时间并在set属性中构建一个新的Date但总是出现溢出错误。

4 个答案:

答案 0 :(得分:3)

你可以这样做:

private DateTime _effectiveDate;
...
public DateTime DateOfEffectiveDate
{
    get { return _effectiveDate; }
    set
    {
        _effectiveDate = new DateTime(value.Year, value.Month, value.Day, _effectiveDate.Hour, _effectiveDate.Minute, _effectiveDate.Second);
    }
}

public DateTime TimeOfEffectiveDate
{
    get { return _effectiveDate; }
    set
    {
        _effectiveDate = new DateTime(_effectiveDate.Year, _effectiveDate.Month, _effectiveDate.Day, value.Hour, value.Minute, value.Second);
    }
}

答案 1 :(得分:3)

您可以使用值转换器来保持值

public class DateConverter : IValueConverter
{
    private DateTime timePickerDate;

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

        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null) return timePickerDate;

        var datePickerDate = ((DateTime)(value));

        // compare relevant parts manually
        if (datePickerDate.Hour != timePickerDate.Hour
            || datePickerDate.Minute != timePickerDate.Minute
            || datePickerDate.Second != timePickerDate.Second)
        {
            // correct the date picker value
            var result = new DateTime(datePickerDate.Year,
                 datePickerDate.Month,
                 datePickerDate.Day,
                 timePickerDate.Hour,
                 timePickerDate.Minute,
                 timePickerDate.Second);

            // return, because this event handler will be executed a second time
            return result;
        }

        return datePickerDate;
    }
}

让两个有问题的控件绑定到一个属性,但让日期选择器使用转换器来覆盖时间。

<Grid Margin="10,5" >        
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>
    <sdk:DatePicker Grid.Column="0" TabIndex="1" Padding="0" SelectedDate="{Binding EffectiveDate, Mode=TwoWay, Converter={StaticResource DateConverter}}" SelectedDateFormat="Short"/>
    <toolkit:TimePicker Grid.Column="1" Value="{Binding EffectiveDate, Mode=TwoWay}" Margin="0" Padding="0" HorizontalAlignment="Left" TabIndex="2" />
</Grid>

答案 2 :(得分:1)

内置的DatePicker控件只选择没有任何特定时间的日期,所以我担心尝试在TextBox中显示所选日期的时间是没有意义的。每次选择新日期时,DatePicker将有效清除时间部分。这就是控制的工作原理。

因此,您应该将DatePicker和TextBox分别绑定到两个不同的源属性,分别为DateTime?TimeSpan

另一种选择是使用另一个选择日期和时间的控件,例如:http://wpftoolkit.codeplex.com/wikipage?title=DateTimePicker

答案 3 :(得分:0)

抓住dateTimePicker1_ValueChanged事件,然后设置binded field value合并datetime部分

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DateTime dt = dateTimePicker1.Value;

            int hour = Convert.ToInt32(textBox1.Text.Split(':')[0]);
            int minute = Convert.ToInt32(textBox1.Text.Split(':')[1]);

            bindedfield = new DateTime(dt.Year, dt.Month, dt.Day, hour, minute, 0);
        }