いつもお世話になります。
前提・実現したいこと
WPFのユーザーコントロール内の一部(ContextMenu)を
ユーザーコントロールの使用元から変更したいのですが、
上記実現する方法が不明なので質問させていただきました。
環境
Visual Studio 2017
C# 4.6.1
WPF
MVVM Lightフレームワーク使用
現状のソースコード
具体的には、下記のようなXamlコードがあり。。。
MainWindow.xaml
xaml
1<Window x:Class="WpfUserControlTest.View.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfUserControlTest.View" 7 mc:Ignorable="d" 8 DataContext="{Binding MainWindow, Source={StaticResource Locator}}" 9 Title="MainWindow" Height="450" Width="800"> 10 <Grid> 11 <Grid.ColumnDefinitions> 12 <ColumnDefinition Width="*" /> 13 <ColumnDefinition Width="*" /> 14 </Grid.ColumnDefinitions> 15 16 <local:CustomListBox Grid.Column="0" Background="AliceBlue" DataContext="{Binding CustomListVM, Source={StaticResource Locator}}" /> 17 <local:CustomListBox Grid.Column="1" Background="Aqua" DataContext="{Binding CustomListVM2, Source={StaticResource Locator}}" /> 18 19 </Grid> 20</Window>
CustomListBox.xaml
xaml
1<UserControl x:Class="WpfUserControlTest.View.CustomListBox" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:local="clr-namespace:WpfUserControlTest.View" 7 mc:Ignorable="d" 8 d:DesignHeight="450" d:DesignWidth="800"> 9 <Grid> 10 11 <ListBox x:Name="_list" 12 Background="{Binding Background, RelativeSource={RelativeSource FindAncestor ,AncestorType={x:Type UserControl}}}" 13 SelectedItem="{Binding SelectedItem}" 14 ItemsSource="{Binding List}" > 15 <ListBox.ItemTemplate> 16 <DataTemplate > 17 <ContentControl> 18 19 <!-- アイテムパネル --> 20 <StackPanel Orientation="Vertical" Background="Transparent" Tag="{Binding Path=DataContext , ElementName=_list}" > 21 <TextBlock Text="{Binding Title}" /> 22 <TextBlock Text="{Binding Data}" /> 23 <StackPanel.ContextMenu> 24 <ContextMenu> 25 <MenuItem Header="削除" 26 CommandParameter="{Binding }" 27 Command="{Binding PlacementTarget.Tag.AssetDeleteCommand , RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" 28 /> 29 </ContextMenu> 30 <!-- 31 <ContextMenu> 32 <MenuItem Header="編集" 33 CommandParameter="{Binding }" 34 Command="{Binding PlacementTarget.Tag.AssetEditCommand , RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" 35 /> 36 </ContextMenu> 37 --> 38 </StackPanel.ContextMenu> 39 </StackPanel> 40 41 </ContentControl> 42 </DataTemplate> 43 </ListBox.ItemTemplate> 44 </ListBox> 45 </Grid> 46</UserControl> 47
上記コードだとListBoxのアイテムを右クリックすると削除項目のみのコンテキストメニューが表示します。
しかしながらこの削除項目が場合によっては編集項目となったり、
「編集」「削除」「開く」が選択できるコンテキストメニューになったりと場合によってコンテキストメニューが変わります
具体的にやりたいコード
最終的にはユーザーコントロールの使用元から下記のように
コンテキストメニューを変更したいです。
MainWindow.xaml
xaml
1<Window x:Class="WpfUserControlTest.View.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfUserControlTest.View" 7 mc:Ignorable="d" 8 DataContext="{Binding MainWindow, Source={StaticResource Locator}}" 9 Title="MainWindow" Height="450" Width="800"> 10 <Grid> 11 <Grid.ColumnDefinitions> 12 <ColumnDefinition Width="*" /> 13 <ColumnDefinition Width="*" /> 14 </Grid.ColumnDefinitions> 15 16 <local:CustomListBox Grid.Column="0" Background="AliceBlue" DataContext="{Binding CustomListVM, Source={StaticResource Locator}}" > 17 <!-- このようにコンテキストメニューを下記のように指定したい --> 18 <ContextMenu> 19 <MenuItem Header="削除" 20 CommandParameter="{Binding }" 21 Command="{Binding PlacementTarget.Tag.AssetDeleteCommand , RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" /> 22 </ContextMenu> 23 </local:CustomListBox> 24 </Grid> 25</Window>
上記のような方法以外でもユーザーコントロール外からユーザーコントロール内部のコンテキストメニューの変更方法等ご存知であればご教示の程よろしくお願いいたします。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/05 10:16
2018/12/05 10:24
2018/12/05 12:00