2015-11-19 83 views
1

我有两个Context Menus像下面,两者都具有不同的控制的相同syntax且资源。两个上下文菜单分享图标,但只有一个上下文菜单图标显示

<ContextMenu ItemsSource="{Binding Actions}"> 
     <ContextMenu.ItemContainerStyle> 
      <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MetroMenuItem}"> 
       <Setter Property="Header" Value="{Binding Title}"/> 
       <Setter Property="ToolTip" Value="{Binding ToolTips}"/> 
       <Setter Property="Command" Value="{Binding Command}"/> 
       <Setter Property="Icon" Value="{Binding Icon}"/> 
       <Setter Property="CommandParameter" Value="{Binding CommandParameter}"/> 
      </Style> 
     </ContextMenu.ItemContainerStyle> 
    </ContextMenu> 

所有的工作正常,但只有问题与图标。

视图模型

我有一个像下面的属性,

public Image Icon 
    { 
     get { return _Icon; } 
     set{ _Icon = value; NotifyPropertyChanged(); } 
    } 

我初始化它像下面,

Icon = new Image 
     { 
      Source = new BitmapImage(new Uri(@"../images/ReIndex.png", UriKind.Relative)), 
      Height = 20, 
      Width = 20, 
      Margin = new Thickness(5) 
     }; 

的问题是,如果一个上下文菜单中显示的图标另一个不会。

我知道MenuItem.IconObject。所以我试图直接使用BitmapImage而不是Image,但仍然有问题。


编辑/解决

添加类似下面的资源,

<Control.Resources>   
    <Image x:Shared="False" x:Key="Icon" Source="{Binding Icon}" Height="20" Width="20"/> 
</Control.Resources> 

然后我的上下文菜单看起来像下面,

<ContextMenu ItemsSource="{Binding Actions}"> 
    <ContextMenu.ItemContainerStyle> 
     <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MetroMenuItem}"> 
      <Setter Property="Header" Value="{Binding Title}"/> 
      <Setter Property="ToolTip" Value="{Binding ToolTips}"/> 
      <Setter Property="Command" Value="{Binding Command}"/> 
      <Setter Property="Icon" Value="{StaticResource Icon}"/> 
      <Setter Property="CommandParameter" Value="{Binding CommandParameter}"/> 
     </Style> 
    </ContextMenu.ItemContainerStyle> 
</ContextMenu> 

视图模型现在

public BitmapImage Icon 
{ 
    get { return _Icon; } 
    set{ _Icon = value; NotifyPropertyChanged(); } 
} 

Icon = new BitmapImage(new Uri(@"../images/Pencil-01.png", UriKind.Relative)); 

要点是x:Shared="False"Image控制。

如果有任何其他好的解决方案表示赞赏。

回答

0

添加类似下面的资源,

<Control.Resources>   
    <Image x:Shared="False" x:Key="Icon" Source="{Binding Icon}" Height="20" Width="20"/> 
</Control.Resources> 

然后我的上下文菜单看起来像下面,

<ContextMenu ItemsSource="{Binding Actions}"> 
    <ContextMenu.ItemContainerStyle> 
     <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MetroMenuItem}"> 
      <Setter Property="Header" Value="{Binding Title}"/> 
      <Setter Property="ToolTip" Value="{Binding ToolTips}"/> 
      <Setter Property="Command" Value="{Binding Command}"/> 
      <Setter Property="Icon" Value="{StaticResource Icon}"/> 
      <Setter Property="CommandParameter" Value="{Binding CommandParameter}"/> 
     </Style> 
    </ContextMenu.ItemContainerStyle> 
</ContextMenu> 

视图模型

public BitmapImage Icon 
{ 
    get { return _Icon; } 
    set{ _Icon = value; NotifyPropertyChanged(); } 
} 

Icon = new BitmapImage(new Uri(@"../images/Pencil-01.png", UriKind.Relative)); 

主要的一点是在Image控制x:Shared="False"