如何获取DatePicker控件的日期值?附相关视图与视图模型代码
如何获取DatePicker控件的日期值?
首先我注意到你当前的View代码里有个小问题:DatePicker被嵌套在Button内部了,这会导致点击DatePicker时意外触发Button的命令,交互逻辑完全不对。先把这个布局问题修正,然后一步步实现日期值的获取:
步骤1:修正View布局
把DatePicker从Button中移出来,让它和Button成为Grid的同级元素,同时给Date的绑定加上Mode=TwoWay(确保用户选择的日期能同步到ViewModel):
<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, Mode=TwoWay}" HorizontalAlignment="Left" Margin="82,331,0,0" VerticalAlignment="Top"/> </Grid>
步骤2:确保ViewModel和数据模型实现INotifyPropertyChanged
WPF/UWP的绑定依赖INotifyPropertyChanged接口来同步UI和数据,所以你的ViewModel和SerieEnCours对应的模型都需要实现这个接口:
数据模型(SerieModel)
public class SerieModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private DateTime _recupHeure; public DateTime RecupHeure { get => _recupHeure; set { _recupHeure = value; OnPropertyChanged(); } } }
完整的ViewModel实现
using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows.Input; using System.Diagnostics; public class YourViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } // 声明SerieEnCours属性 private SerieModel _serieEnCours; public SerieModel SerieEnCours { get => _serieEnCours; set { _serieEnCours = value; OnPropertyChanged(); } } // 你的RelayCommand public ICommand RecuperationDateTime { get; private set; } public YourViewModel() { // 初始化数据模型和命令 SerieEnCours = new SerieModel(); RecuperationDateTime = new RelayCommand(ExecuteRecuperationDateTime); } // 命令执行方法:在这里获取日期值 private void ExecuteRecuperationDateTime() { DateTime selectedDate = SerieEnCours.RecupHeure; // 这里可以添加你的业务逻辑,比如打印、保存日期 Debug.WriteLine($"用户选择的日期是: {selectedDate:yyyy-MM-dd HH:mm:ss}"); } }
步骤3:确认RelayCommand的实现
如果你的RelayCommand还没完整实现,这里给一个适配WPF/UWP的基础版本:
public class RelayCommand : ICommand { private readonly Action _execute; private readonly Func<bool> _canExecute; public event EventHandler CanExecuteChanged; public RelayCommand(Action execute, Func<bool> canExecute = null) { _execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute; } public bool CanExecute(object parameter) { return _canExecute == null || _canExecute(); } public void Execute(object parameter) { _execute(); } public void RaiseCanExecuteChanged() { CanExecuteChanged?.Invoke(this, EventArgs.Empty); } }
关键注意点
- 双向绑定:
Mode=TwoWay是必须的,否则用户在DatePicker中选择的日期不会同步到ViewModel的RecupHeure属性。 - 接口实现:
INotifyPropertyChanged确保属性变化时UI能响应,同时UI的变化也能反向同步到数据。 - 布局逻辑:控件嵌套要合理,Button是点击触发控件,不要把交互控件嵌套在里面。
这样当用户点击"Retour"按钮时,ExecuteRecuperationDateTime方法就会拿到DatePicker选择的日期值了。
内容的提问来源于stack exchange,提问作者CorentinMerit
相关产品推荐
相关产品推荐

