본문 바로가기
카테고리 없음

05. WPF MVVM(4) ContextMenuMap 동적할당

by NaHyungMin 2020. 8. 21.

2020/08/21 - [WPF] - 02. WPF MVVM(1) View-ViewModel 연결

2020/08/21 - [WPF] - 03. WPF MVVM(2) INotifyPropertyChanged

2020/08/21 - [WPF] - 04. WPF MVVM(3) RelayCommand

 

public AllotmentView ViewWindow { get; set; } 
public DataTable Grade1Grid { get { return grade1Grid; } set { grade1Grid = value; OnPropertyChanged(); } } 
public DataRowView GradeRowView { set { GradeGrideClick(value); OnPropertyChanged(); } } 
public ICommand BasicsButtonCommand { get; set; } 

public AllotmentViewModel(AllotmentView viewWindow) 
{ 
	this.ViewWindow = viewWindow; 
    BasicsButtonCommand = new RelayCommand<Button>(BasicsClick, CanExcuteMethod); 
} 

private void BasicsClick(Button sender) 
{
	//이벤트
} 
private bool CanExcuteMethod(object arg) 
{ 
	//활성화 return true;
}

 

위에 항목에 다음 내용을 추가한다.

 

 public ObservableCollection<MenuItemViewModel> ContextMenuMap
 {
 	get { return contextMenuMap; }
 	set
	 {
 		contextMenuMap = value;
 		OnPropertyChanged();
 	}
 }

private ObservableCollection<MenuItemViewModel> contextMenuMap;

 

그리고 생성자에 아래 내용을 추가

ContextMenuMap = new ObservableCollection<MenuItemViewModel>
{
	new MenuItemViewModel("배정", SetAllotment, () => {return true; }),
	new MenuItemViewModel("취소", SetAllotmentCancel, () => {return true; }),
};

ViewWindow.grade1Grid.ContextMenu = new ContextMenu()
{
	ItemsSource = ContextMenuMap,
};

 

최종은 아래와 같다.

 

public AllotmentView ViewWindow { get; set; } 
public DataTable Grade1Grid { get { return grade1Grid; } set { grade1Grid = value; OnPropertyChanged(); } } 
public DataRowView GradeRowView { set { GradeGrideClick(value); OnPropertyChanged(); } } 
public ICommand BasicsButtonCommand { get; set; } 

 public ObservableCollection<MenuItemViewModel> ContextMenuMap
 {
 	get { return contextMenuMap; }
 	set
	 {
 		contextMenuMap = value;
 		OnPropertyChanged();
 	}
 }

private ObservableCollection<MenuItemViewModel> contextMenuMap;

public AllotmentViewModel(AllotmentView viewWindow) 
{ 
	this.ViewWindow = viewWindow; 
    BasicsButtonCommand = new RelayCommand<Button>(BasicsClick, CanExcuteMethod); 
    
    ContextMenuMap = new ObservableCollection<MenuItemViewModel>
	{
	new MenuItemViewModel("배정", SetAllotment, () => {return true; }),
	new MenuItemViewModel("취소", SetAllotmentCancel, () => {return true; }),
	};

	ViewWindow.grade1Grid.ContextMenu = new ContextMenu()
	{
		ItemsSource = ContextMenuMap,
	};
} 

private void BasicsClick(Button sender) 
{
	//이벤트
} 
private bool CanExcuteMethod(object arg) 
{ 
	//활성화 return true;
}

private void SetAllotment()
{
}

private void SetAllotmentCancel()
{
}
public class MenuItemViewModel : MenuItem
    {
        //public string Header { get; private set; }
        //public ICommand Command { get; private set; }

        public MenuItemViewModel(string header, Action execute, Func<bool> canExecute = null)
        {
            this.Header = header;

            if (execute != null)
                this.Command = new RelayCommand(execute, canExecute);
        }

        public MenuItemViewModel(string name, string header, Action execute, Func<bool> canExecute = null)
        {
            this.Header = header;
            this.Name = name;

            if(execute != null)
                this.Command = new RelayCommand(execute, canExecute);
        }
    }

    public class MenuItemViewModel<T> : MenuItem
    {
        public MenuItemViewModel(string header, Action<T> execute)
        {
            this.Header = header;

            if (execute != null)
                this.Command = new RelayCommand<T>(execute);
        }

        public MenuItemViewModel(string name, string header, Action<T> execute)
        {
            this.Header = header;
            this.Name = name;
            this.CommandParameter = this;

            if (execute != null)
                this.Command = new RelayCommand<T>(execute);
        }
    }