Visual Studio Community 2017 で C# の WPF アプリ (.NET Framework) を作って実行しました。
想定では「File」メニューをクリックすると「新規作成」「開く...」の二つの項目が表示され、各メニュー項目にはアイコンが表示されるはずでした。
ところが、項目は両方とも表示されるものの、新規作成のアイコンが表示されません。
しかし、MainWindow.xaml から開くメニュー項目を表す「<MenuItem Style="{StaticResource CommandMenu}" DataContext="{Binding Commands[1]}"/>
」を削除すると新規作成のアイコンは表示されます。
どうしてこのようになるのでしょうか?
ソースは以下の通りで、新規作成メニュー項目のアイコンは Resources/New.png として、開くメニュー項目のアイコンは Resources/Open.png としてプロジェクトに追加されており、両方ともビルドアクションは Resource になっています。
MainWindow.xaml
XML
1<Window x:Class="WpfApp1.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:WpfApp1" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="350" Width="525"> 9 <Grid> 10 <Grid.RowDefinitions> 11 <RowDefinition Height="Auto"/> 12 <RowDefinition/> 13 </Grid.RowDefinitions> 14 <Menu Grid.Row="0"> 15 <Menu.Resources> 16 <Style TargetType="MenuItem" x:Key="CommandMenu"> 17 <Setter Property="Command" Value="{Binding}"/> 18 <Setter Property="Icon"> 19 <Setter.Value> 20 <Image Source="{Binding ImageSource}"/> 21 </Setter.Value> 22 </Setter> 23 <Setter Property="Header" Value="{Binding Text}"/> 24 </Style> 25 </Menu.Resources> 26 <MenuItem Header="File"> 27 <MenuItem Style="{StaticResource CommandMenu}" DataContext="{Binding Commands[0]}"/> 28 <MenuItem Style="{StaticResource CommandMenu}" DataContext="{Binding Commands[1]}"/> 29 </MenuItem> 30 </Menu> 31 </Grid> 32</Window>
MainWindow.xaml.cs
C#
1using System.Windows; 2 3namespace WpfApp1 4{ 5 /// <summary> 6 /// MainWindow.xaml の相互作用ロジック 7 /// </summary> 8 public partial class MainWindow : Window 9 { 10 public MainWindow() 11 { 12 InitializeComponent(); 13 DataContext = new ViewModel(); 14 } 15 } 16}
ViewModel.cs
C#
1using System.Collections.ObjectModel; 2using System.Windows; 3 4namespace WpfApp1 5{ 6 class ViewModel : DependencyObject 7 { 8 public ViewModel() 9 { 10 Commands = new ObservableCollection<Command>() 11 { 12 new Command("Resources/New.png"){ Text="新規作成" }, 13 new Command("Resources/Open.png"){ Text="開く..." } 14 }; 15 } 16 17 #region Commands 18 public static readonly DependencyProperty CommandsProperty = 19 DependencyProperty.Register(nameof(Commands), typeof(ObservableCollection<Command>), typeof(ViewModel)); 20 public ObservableCollection<Command> Commands 21 { 22 get => (ObservableCollection<Command>)GetValue(CommandsProperty); 23 set => SetValue(CommandsProperty, value); 24 } 25 #endregion 26 } 27}
Command.cs
C#
1using System; 2using System.Windows; 3using System.Windows.Input; 4using System.Windows.Media; 5using System.Windows.Media.Imaging; 6 7namespace WpfApp1 8{ 9 class Command : DependencyObject, ICommand 10 { 11 public Command(string imageUri) 12 { 13 ImageSource = new BitmapImage(new Uri(imageUri, UriKind.Relative)); 14 } 15 16 #region Text 17 public static readonly DependencyProperty TextProperty = 18 DependencyProperty.Register(nameof(Text), typeof(string), typeof(Command)); 19 public string Text 20 { 21 get => (string)GetValue(TextProperty); 22 set => SetValue(TextProperty, value); 23 } 24 #endregion 25 26 #region ImageSource 27 public static readonly DependencyProperty ImageSourceProperty = 28 DependencyProperty.Register(nameof(ImageSource), typeof(ImageSource), typeof(Command)); 29 public ImageSource ImageSource 30 { 31 get => (ImageSource)GetValue(ImageSourceProperty); 32 set => SetValue(ImageSourceProperty, value); 33 } 34 #endregion 35 36 public event EventHandler CanExecuteChanged; 37 protected virtual void OnCanExecuteChanged() 38 { 39 CanExecuteChanged?.Invoke(this, new EventArgs()); 40 } 41 42 public bool CanExecute(object parameter) 43 { 44 return true; 45 } 46 47 public void Execute(object parameter) 48 { 49 MessageBox.Show(Text); 50 } 51 } 52}

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