ContextMenuの中身をMVVMで動的に作成する
2021-03-14
MainView.xaml
<Window x:Class="WpfApp15.View.MainView"
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"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
xmlns:local="clr-namespace:WpfApp15.View"
xmlns:vm="clr-namespace:WpfApp15.ViewModel"
mc:Ignorable="d"
Title="MainView" Height="450" Width="800">
<Window.DataContext>
<vm:MainViewModel/>
</Window.DataContext>
<i:Interaction.Triggers>
<i:EventTrigger EventName ="Loaded">
<l:LivetCallMethodAction MethodTarget="{Binding}" MethodName="OnLoaded" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<Button>
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="MenuParent" ItemsSource="{Binding ArrayMenu1}">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding DisplayName}"/>
<Setter Property="IsChecked" Value="{Binding Checked}"/>
<Setter Property="Command" Value="{Binding}"/>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
</Grid>
</Window>
MainViewModel.cs
using System;
using System.Collections.Generic;
using System.Windows;
namespace WpfApp15.ViewModel
{
public class MainViewModel : Livet.ViewModel
{
/// <summary>
/// コンテキストメニュー
/// </summary>
private List<ContextMenuClass> _ArrayMenu1 = new List<ContextMenuClass>();
public List<ContextMenuClass> ArrayMenu1
{
get
{
return _ArrayMenu1;
}
set
{
_ArrayMenu1 = value;
RaisePropertyChanged();
}
}
/// <summary>
/// 起動時処理
/// </summary>
public void OnLoaded()
{
for(int intCount = 0; intCount < 5; intCount++)
{
ContextMenuClass contextMenu = new ContextMenuClass("Menu" + intCount, intCount == 0 ? true : false);
contextMenu.ExecuteEventHandler += (object sender, EventArgs e)=>
{
foreach(ContextMenuClass Menu in ArrayMenu1)
{
Menu.Checked = Menu == sender ? true : false;
}
MessageBox.Show((sender as ContextMenuClass).DisplayName + " Selected");
};
ArrayMenu1.Add(contextMenu);
}
}
}
}
ContextMenuClass.cs
using System;
using System.Windows.Input;
namespace WpfApp15.ViewModel
{
/// <summary>
/// コンテキストメニュークラス
/// </summary>
public class ContextMenuClass : Livet.ViewModel, ICommand
{
public ContextMenuClass(string strDisplayName, bool bolChecked)
{
this.DisplayName = strDisplayName;
this.Checked = bolChecked;
}
private string _DisplayName = "";
public string DisplayName
{
get
{
return _DisplayName;
}
set
{
_DisplayName = value;
RaisePropertyChanged();
}
}
private bool _Checked = false;
public bool Checked
{
get
{
return _Checked;
}
set
{
_Checked = value;
RaisePropertyChanged();
}
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
// 選択時イベントハンドラ
public event EventHandler ExecuteEventHandler;
public void Execute(object parameter)
{
if (ExecuteEventHandler != null)
ExecuteEventHandler(this, EventArgs.Empty);
}
}
}
ディスカッション
コメント一覧
まだ、コメントがありません