我怎样才能获得datepicker的价值

时间:2018-03-20 14:51:21

标签: c# uwp

我会得到我的日期选择器的日期值。

查看:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button Command="{Binding RecuperationDateTime}" Content="Retour" HorizontalAlignment="Left" Margin="82,567,0,0" VerticalAlignment="Top"/>
        <DatePicker Name="heurePicker" Date="{Binding SerieEnCours.RecupHeure}" HorizontalAlignment="Left" Margin="82,331,0,0" VerticalAlignment="Top"/>
 </Grid>

ViewModel:

`public RelayCommand RecuperationDateTime{ get; private set; }

 private lenovo serieEnCours;

    public lenovo SerieEnCours
    {
        get
        {
            return serieEnCours;
        }

        set
        {
            if (value != serieEnCours)
            {
                serieEnCours = value;
                NotifyPropertyChanged();
            }
        }
    }

    public VM_lenvo(INavigationService navigationService)
    {
        SerieEnCours = new lenovo()
        {
            TarifEnCours = 0,
            RecupHeure = DateTime.Now
        };

        _navigationService = navigationService;
        RecuperationDateTime= new RelayCommand(_RecuperationDateTime);
    }

    private void _RecuperationDateTime()
    {
        DateTime heure = SerieEnCours.RecupHeure;
        //Return
        _navigationService.GoBack();
    }`

Modele:

 DateTime HeureRecuperer;
 public event PropertyChangedEventHandler PropertyChanged;
 public DateTime RecupHeure
 {
    get
            {
                return HeureRecuperer;
            }

            set
            {
                if (value != HeureRecuperer)
                {
                    HeureRecuperer = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("RecupHeure"));
                    }
                }
           }
        }

当我生成我的代码时,我有一个“错误”。 我得到了我的实际日期...

1 个答案:

答案 0 :(得分:0)

如果您想让用户选择日期,则必须使用TwoWay数据绑定:

{Binding SerieEnCours.RecupHeure, Mode=TwoWay}

默认情况下,绑定仅观察视图模型中的更改并将其传输到UI,但不是相反。添加TwoWay模式时,它还会反映视图模型中的UI更改。