如何使用Bindings获取DateTime值? (WPF-MVVM)

时间:2021-09-03 15:53:47

I would like something like this:

我想要这样的东西:

<DatePicker SelectedDate="{Binding StartDate}" />

Is there any control or easy solution for this? (I use MVVM.)

对此有任何控制或简单的解决方案吗? (我使用MVVM。)

2 个解决方案

#1


9  

In this case you must have only property StartDate in ViewModel and it will be working.

在这种情况下,您必须在ViewModel中只有属性StartDate,它才能正常工作。

And when you change date in the DatePicker, it will be automatically reflected in the property StartDate in ViewModel class.

当您在DatePicker中更改日期时,它将自动反映在ViewModel类的属性StartDate中。

Simple ViewModel:

class MainViewModel : INotifyPropertyChanged
    {
        private DateTime _startDate = DateTime.Now;
        public DateTime StartDate
        {
            get { return _startDate; }
            set { _startDate = value; OnPropertyChanged("StartDate");  }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }
    }

Simple View:

<Window x:Class="SimpleBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>    
        <DatePicker SelectedDate="{Binding StartDate}" />        
    </StackPanel>
</Window>

Code-behind:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();         
            this.DataContext = new MainViewModel();
        }        
    }

#2


1  

See the WPF Toolkit (note: CodePlex is down/slow at the time of writing).

请参阅WPF工具包(注意:撰写本文时CodePlex已关闭/慢速)。

#1


9  

In this case you must have only property StartDate in ViewModel and it will be working.

在这种情况下,您必须在ViewModel中只有属性StartDate,它才能正常工作。

And when you change date in the DatePicker, it will be automatically reflected in the property StartDate in ViewModel class.

当您在DatePicker中更改日期时,它将自动反映在ViewModel类的属性StartDate中。

Simple ViewModel:

class MainViewModel : INotifyPropertyChanged
    {
        private DateTime _startDate = DateTime.Now;
        public DateTime StartDate
        {
            get { return _startDate; }
            set { _startDate = value; OnPropertyChanged("StartDate");  }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }
    }

Simple View:

<Window x:Class="SimpleBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>    
        <DatePicker SelectedDate="{Binding StartDate}" />        
    </StackPanel>
</Window>

Code-behind:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();         
            this.DataContext = new MainViewModel();
        }        
    }

#2


1  

See the WPF Toolkit (note: CodePlex is down/slow at the time of writing).

请参阅WPF工具包(注意:撰写本文时CodePlex已关闭/慢速)。