Only date and time binding

I have 2 controls:

DatePicker: for date only
TextBox with TimeMask: only for time

They are both related to the same property. DateTime EffectiveDate But the problem is that I change the date to DatePicker, it changes the time in the TimeTextBox to 12:00.

I understand the reason for this, but what better solution is there to allow them to work separately, but tied to the same property?

I tried to take the current time and create a new Date in the set property, but always end up with overflow errors.

+4
source share
4 answers

You can use the value converter to save the value

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>
+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);
    }
}
+3

DatePicker - , , TextBox. DatePicker , . .

, DatePicker TextBox : DateTime? TimeSpan .

- , , : http://wpftoolkit.codeplex.com/wikipage?title=DateTimePicker

+1

dateTimePicker1_ValueChanged, binded field value date time

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);
        }
0

Source: https://habr.com/ru/post/1668378/


All Articles