如何调试setter不在Timepicker属性上触发?

时间:2016-04-03 17:47:04

标签: c# datetime windows-phone-8.1 timepicker

概述: 我已经为我的UI添加了一个Timepicker控件,并将Time属性绑定设置为关联的ViewModel上的DateTime属性。

但是当我调试解决方案时,不会为属性 <TimePicker Grid.Row="2" Grid.Column="1" Width="270" Height="100" HorizontalAlignment="Center" VerticalAlignment="Bottom" Header="Parking Duration" Time="{Binding SelectedParkDuration}" /> 调用setter。

为了进一步调试,我检查了UI的数据上下文。正确设置数据上下文并调用Timepicker的getter。

问题: 有没有人知道为什么在运行时选择时间选择器上的值时调用者不会被调用?

时间选择器控件定义:

    private DateTime _selectedParkDuration;
    public DateTime SelectedParkDuration
    {
        get
        {
            return this._selectedParkDuration;
        }

        set
        {
            if (_selectedParkDuration != value)
            {
                _selectedParkDuration = value;
                RaisePropertyChanged("SelectedParkDuration");
            }
        }
    }

时间选择器属性 - 在UI的ViewModel中定义的SelectedParkDuration:

.+

1 个答案:

答案 0 :(得分:1)

The solution is to specify two-way binding as @Ken Tucker suggested. Also the type of the property needed to be of type TimeSpan:

    private TimeSpan? _selectedParkDuration;
    public TimeSpan? SelectedParkDuration
    {
        get
        {
            return this._selectedParkDuration;
        }

        set
        {
            if (_selectedParkDuration != value)
            {
                _selectedParkDuration = value;
                RaisePropertyChanged("SelectedParkDuration");
            }
        }
    }

Xaml definition of TimePicker:

<TimePicker Grid.Row="2"
                        Grid.Column="1"
                        Width="270"
                        Height="100"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Bottom"
                        Header="Parking Duration"
                        Time="{Binding SelectedParkDuration,
                                       Mode=TwoWay}" />