加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何将DateDicker中的SelectedDateChanged事件绑定到VM中

发布时间:2020-12-16 01:43:28 所属栏目:百科 来源:网络整理
导读:我有一个带有空代码隐藏文件的wpf文件(如果可能,我想保持代码隐藏为空) http://pastebin.com/x1CTZDFK 然后我有了这个viewmodel文件 using System;using System.Collections.ObjectModel;using System.Windows.Input;using MVVM_Test.Model;using MvvmFounda
我有一个带有空代码隐藏文件的wpf文件(如果可能,我想保持代码隐藏为空)

http://pastebin.com/x1CTZDFK

然后我有了这个viewmodel文件

using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using MVVM_Test.Model;
using MvvmFoundation.Wpf;

namespace MVVM_Test.ViewModel
{
    public class ViewModel : ObservableObject
    {
        private DateTime selectedDate;
        public DateTime SelectedDate
        {
            get
            {
                return selectedDate;
            }
            set
            {
                selectedDate = value;
                RaisePropertyChanged("SelectedDate");
            }
        }

        private DateTime startDate;
        public DateTime StartDate
        {
            get { return startDate; }
            set
            {
                startDate = value;
                RaisePropertyChanged("StartDate");
            }
        }

        private DateTime endDate;
        public DateTime EndDate
        {
            get { return endDate; }
            set
            {
                endDate = value;
                RaisePropertyChanged("EndDate");
            }
        }

        public ObservableCollection<Brick> SavedBricks { get; set; }

        public ViewModel()
        {
            SelectedDate = DateTime.Now;
            StartDate = new DateTime(2011,1,1);
            EndDate = new DateTime(2011,7,31);
            SavedBricks = new ObservableCollection<Brick>();
            //Brick b1 = new Brick(DateTime.Now,50,300);
            //SavedBricks.Add(b1);
        }

        public ICommand PrevHistory_cmd
        {
            get { return new RelayCommand(PrevHistoryExecute,PrevHistoryCanExecute); }
        }

        private void PrevHistoryExecute()
        {
            SelectedDate = SelectedDate - new TimeSpan(1,0);
        }

        private bool PrevHistoryCanExecute()
        {
            if (StartDate < SelectedDate)
                return true;
            return false;
        }

        public ICommand NextHistory_cmd
        {
            get { return new RelayCommand(NextHistoryExecute,NextHistoryCanExecute); }
        }

        private void NextHistoryExecute()
        {
            SelectedDate = SelectedDate + new TimeSpan(1,0);
        }

        private bool NextHistoryCanExecute()
        {
            if(EndDate > SelectedDate)
                return true;
            return false;
        }

        public ICommand StartStopSort_cmd
        {
            get { return new RelayCommand(NextHistoryExecute,NextHistoryCanExecute); }
        }

        private void StartStopSortExecute()
        {

        }

        private bool StartStopSortCanExecute()
        {
            return true;
        }

        public ICommand DateSelectionChanged_cmd
        {
            get { return new RelayCommand(NextHistoryExecute,NextHistoryCanExecute); }
        }

        private void DateSelectionChangedExecute()
        {

        }

        private bool DateSelectionChangedCanExecute()
        {
            return true;
        }
    }
}

然后,我希望SelectedDateChanged事件执行我的公共ICommand DateSelectionChanged_cmd,以便我可以在我的视图模型中验证它.我试着让System.Windows.Interactivity工作但是当我把它作为参考添加它不会编译并说它无法找到该文件,所以现在我想知道是否还有其他方法可以做到这一点,我写的方式是xml文件只是我猜它会看起来的一个例子(这是错误的方式)

解决方法

您无法将事件直接绑定到命令.您可以使用附加行为,如 here所示,但实际上您不需要:由于绑定SelectedDate是双向的,您只需要在SelectedDate属性的setter中执行DateSelectionChangedExecute:

public DateTime SelectedDate
    {
        get
        {
            return selectedDate;
        }
        set
        {
            selectedDate = value;
            RaisePropertyChanged("SelectedDate");
            DateSelectionChangedExecute();
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读