从'subclass'执行RaiseCanExecuteChanged

时间:2015-09-18 21:01:26

标签: wpf relaycommand

所以我有以下设置:

规划:

public class Planning : ViewModelBase
{
    public Planning()
    {
        AddNewActivityCommand = new RelayCommand(AddActivity, CanAddActivity);
    }

    public ObservableCollection<PlanningItem> PlanningItems { get; set; }

    public PlanningItem SelectedPlan { get; set; }

    #region AddNewActivity
    public RelayCommand AddNewActivityCommand { get; private set; }

    private bool CanAddActivity()
    {
        if (!PlanningItems.Any())
        {
            return true;
        }
        if (string.IsNullOrEmpty(PlanningItems[PlanningItems.Count - 1].Activities) != true ||
            PlanningItems[PlanningItems.Count - 1].DhpRepresentativeSelected != null)
            {
                return true;
            }
        return false;
    }

    private void AddActivity()
    {
        PlanningItems.Add(new PlanningItem());
        AddNewActivityCommand.RaiseCanExecuteChanged();
    }
    #endregion
}

规划项目:

public class PlanningItem : ViewModelBase
{
    private string _activity;

    public ObservableCollection<OutreachUser> DhpRepresentativeSource
    {
        get
        {
            var userSource = new ObservableCollection<OutreachUser>();

            using (var context = new Outreach_Entities())
            {
                var query = from a in context.UserInfoes
                            join b in context.PersonalInfoes on a.UserIdentity equals b.PersonIdentity
                            join c in context.PersonalTitles on b.TitleLink equals c.TitleIdentity into cGroup
                            from c in cGroup.DefaultIfEmpty()
                            select new OutreachUser
                            {
                                PersonLink = a.UserIdentity,
                                Username = a.Username,
                                FirstName = b.FirstName,
                                MiddleInitial = b.MiddleInitial,
                                LastName = b.LastName
                            };
                foreach (var result in query)
                {
                    userSource.Add(result);
                }
                return userSource;
            }
        }
    }

    public OutreachUser DhpRepresentativeSelected { get; set; }

    public DateTime PlanningDate { get; set; }

    public TimeSpan PlanningStart { get; set; }

    public TimeSpan PlanningEnd { get; set; }

    public int PlanningTotalHours { get; set; }

    public string Activities
    {
        get
        {
            return _activity;
        }
        set
        {
            _activity = value;
            RaisePropertyChanged(nameof(Activities), "", _activity, true);
        }
    }
}

我有一个绑定到PlanningItems Observable Collection的ListBox。 如果满足以下条件,我希望能够将新项目添加到列表中:

  1. 规划项目集合为空。
  2. 规划项目集合中的最后一项具有非空的DhpRepresentativeSelected。
  3. “计划项目集合”中的最后一项在“活动”字符串中有一些文本。
  4. 第一项很容易,因为我在空列表中添加新项目后调用AddNewActivityCommand.RaiseCanExecuteChanged();

    现在我需要从PlanningItem ViewModel中调用AddNewActivityCommand.RaiseCanExecuteChanged();,但它没有该命令的访问权限。

1 个答案:

答案 0 :(得分:0)

Clueless向我指出答案。

我在Planning ViewModel中做了什么我创建了一个调用AddNewActivityCommand.RaiseCanExecuteChanged()方法的内部方法。我认为在PlanningItems ViewModel中调用了该方法。