2017-02-23 134 views
0

我在我的WPF MVVM應用圖標菜單項不顯示

<Menu DockPanel.Dock="Top" > 
    <MenuItem Header="File"> 
     <MenuItem Header="Open Video..." 
         ToolTip="Open Video File..." 
         Caliburn:Message.Attach="[Event Click] = [Action OpenFile()]" 
         InputGestureText="Ctrl + O"> 
      <MenuItem.Icon> 
      <Image Width="16" Height="16" 
        Source="pack://application:,,,/Redactor;component/Resources/Pngs/open_document_black_16.png"/> 
      </MenuItem.Icon> 
     </MenuItem> 
    </MenuItem> 
    <MenuItem Header="Actions" ItemsSource="{Binding DynamMenuItems}"> 
     <MenuItem.Resources> 
      <Image x:Key="FrozenImage" x:Shared="False" Source="{Binding Path=Image}"/> 
     </MenuItem.Resources> 
     <MenuItem.ItemContainerStyle> 
      <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MetroMenuItem}"> 
       <!--<Setter Property="Icon" Value="{Binding Path=Image}"/>--> 
       <Setter Property="Icon" Value="{StaticResource FrozenImage}"/> 
       <Setter Property="Header" Value="{Binding Path=Text}"/> 
       <Setter Property="Command" Value="{Binding Path=Command}"/> 
      </Style> 
     </MenuItem.ItemContainerStyle> 
    </MenuItem> 
</Menu> 

的視圖在視圖模型下面的菜單我有以下代碼

private void BuildSelectionContextMenuItems() 
{ 
    if (RedactionMenuItems == null) 
     RedactionMenuItems = new BindableCollection<ContextMenuItem>(); 

    RedactionMenuItems.Clear(); 

    RedactionMenuItems.Add(new ContextMenuItem(
     Utils.GetImageFromUrl("pack://application:,,,/Redactor;component/Resources/Pngs/save_black_16.png"), 
     "Save selection to video file...", 
     SaveSelectionToVideoFile)); 

    RedactionMenuItems.Add(new ContextMenuItem(
     "Commence auto-redaction on the selection...", 
     PerformAutoRedactionOnSelection)); 

    RedactionMenuItems.Add(new ContextMenuItem(
     "Create matching audio-interval for selection", 
     CreateAudioIntervalFromSelection)); 
} 

private BindableCollection<ContextMenuItem> redactionMenuItems; 
private ICommand saveSelectionToVideoFile; 
private ICommand performAutoRedactionOnSelection; 
private ICommand createAudioIntervalFromSelection; 

public BindableCollection<ContextMenuItem> RedactionMenuItems 
{ 
    get { return redactionMenuItems; } 
    set 
    { 
     redactionMenuItems = value; 
     NotifyOfPropertyChange(() => RedactionMenuItems); 
    } 
} 

這工作,並顯示菜單項目,但該圖標不顯示,當我的XAML代碼更改

<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MetroMenuItem}"> 
    <Setter Property="Icon" Value="{Binding Path=Image}"/> 
    <Setter Property="Header" Value="{Binding Path=Text}"/> 
    <Setter Property="Command" Value="{Binding Path=Command}"/> 
</Style> 

它顯示了一次,然後disappea RS。所以我嘗試使用已知的方法使圖標非共享使用x:Shared="False"。但我不明白爲什麼這不起作用,因爲我也使用相同的RedactionMenuItems綁定到控件上的ContextMenu,這工作正常。爲什麼這段代碼不工作?


ContextMenuItem的類是

public class ContextMenuItem : PropertyChangedBase 
{ 
    private Image image; 
    private string text; 
    private ICommand command; 

    public ContextMenuItem(Image image, string text, ICommand command) 
    { 
     Image = image; 
     Text = text; 
     Command = command; 
    } 
    public ContextMenuItem(string text, ICommand command) : this(null, text, command) { } 

    public Image Image 
    { 
     get { return image; } 
     set 
     { 
      image = value; 
      NotifyOfPropertyChange(() => Image); 
     } 
    } 

    public string Text 
    { 
     get { return text; } 
     set 
     { 
      text = value; 
      NotifyOfPropertyChange(() => Text); 
     } 
    } 

    public ICommand Command 
    { 
     get { return command; } 
     set 
     { 
      command = value; 
      NotifyOfPropertyChange(() => Command); 
     } 
    } 
} 

回答

1

XAML中的這一部分:

<MenuItem.Resources> 
    <Image x:Key="FrozenImage" x:Shared="False" Source="{Binding Path=Image}"/> 
</MenuItem.Resources> 

被結合Source屬性到Image對象在視圖模型。 Source屬性必須綁定到ImageSource對象。所以,你有2個解決方案:

  1. 在您的視圖模型,改變你的Image屬性的類型是ImageSource類型。注意:您需要創建一個合適的ImageSource對象才能返回。我用了一個BitmapImage像這樣:

    var image1 = new Image(); 
    image1.Source = new BitmapImage(new Uri("/Images/print.png", UriKind.Relative)); 
    
  2. 創建Converter對象圖像屬性轉換爲ImageSource對象。