0

So I have the following setup:

PLANNING:

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
}

PLANNING ITEM:

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);
        }
    }
}

I have a ListBox bound to the PlanningItems Observable Collection. I want to be able to add a new item to the list if the following criteria are met:

  1. The Planning Items Collection is empty.
  2. The last item in the Planning Items Collection has a DhpRepresentativeSelected that is not null.
  3. The last item in the Planning Items Collection has some text in the Activities string.

The first item is easy enough because I call AddNewActivityCommand.RaiseCanExecuteChanged(); after I add a new item from an empty list.

Now I need to call the AddNewActivityCommand.RaiseCanExecuteChanged(); from within the PlanningItem ViewModel, but it does not have access rights to the command.

cmircovich
  • 215
  • 3
  • 13
  • 1
    You can handle that from 2 directions: 1. wrap AddNewActivityCommand.RaiseCanExecuteChanged(); in a method and call it from the PlanningItem. 2. Planning will register to all PlanningItem PropertyChanged notification and will verify the condition when that specific propertyChanged. – Clueless Sep 18 '15 at 21:17

1 Answers1

0

Clueless pointed me to the answer.

What I did was inside of my Planning ViewModel I created an internal Method that called the AddNewActivityCommand.RaiseCanExecuteChanged() method. I think called that method from within the PlanningItems ViewModel.

cmircovich
  • 215
  • 3
  • 13