Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how I made a ViewModel for a menu item: <a href="http://svn.soapboxcore.com/svn/trunk/SoapBox/SoapBox.Core/SoapBox.Core.Contracts/Gui/MenuItem/AbstractMenuItem.cs" rel="nofollow noreferrer">AbstractMenuItem</a>. Pay particular attention to the Icon region:</p> <pre><code> #region " Icon " /// &lt;summary&gt; /// Optional icon that can be displayed in the menu item. /// &lt;/summary&gt; public object Icon { get { if (IconFull != null) { System.Windows.Controls.Image img = new System.Windows.Controls.Image(); if (EnableCondition.Condition) { img.Source = IconFull; } else { img.Source = IconGray; } return img; } else { return null; } } } private BitmapSource IconFull { get { return m_IconFull; } set { if (m_IconFull != value) { m_IconFull = value; if (m_IconFull != null) { IconGray = ConvertFullToGray(m_IconFull); } else { IconGray = null; } NotifyPropertyChanged(m_IconArgs); } } } private BitmapSource m_IconFull = null; static readonly PropertyChangedEventArgs m_IconArgs = NotifyPropertyChangedHelper.CreateArgs&lt;AbstractMenuItem&gt;(o =&gt; o.Icon); private BitmapSource IconGray { get; set; } private BitmapSource ConvertFullToGray(BitmapSource full) { FormatConvertedBitmap gray = new FormatConvertedBitmap(); gray.BeginInit(); gray.Source = full; gray.DestinationFormat = PixelFormats.Gray32Float; gray.EndInit(); return gray; } /// &lt;summary&gt; /// This is a helper function so you can assign the Icon directly /// from a Bitmap, such as one from a resources file. /// &lt;/summary&gt; /// &lt;param name="value"&gt;&lt;/param&gt; protected void SetIconFromBitmap(System.Drawing.Bitmap value) { BitmapSource b = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( value.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); IconFull = b; } #endregion </code></pre> <p>You just derive from this class and in the constructor you call SetIconFromBitmap and pass in a picture from your resx file.</p> <p>Here's how I bound to those IMenuItems in the <a href="http://svn.soapboxcore.com/svn/trunk/SoapBox/SoapBox.Core/SoapBox.Core.Workbench/Workbench/WorkBenchView.xaml" rel="nofollow noreferrer">Workbench Window</a>:</p> <pre><code> &lt;Menu DockPanel.Dock="Top" ItemsSource="{Binding Path=(local:Workbench.MainMenu)}"&gt; &lt;Menu.ItemContainerStyle&gt; &lt;Style&gt; &lt;Setter Property="MenuItem.Header" Value="{Binding Path=(contracts:IMenuItem.Header)}"/&gt; &lt;Setter Property="MenuItem.ItemsSource" Value="{Binding Path=(contracts:IMenuItem.Items)}"/&gt; &lt;Setter Property="MenuItem.Icon" Value="{Binding Path=(contracts:IMenuItem.Icon)}"/&gt; &lt;Setter Property="MenuItem.IsCheckable" Value="{Binding Path=(contracts:IMenuItem.IsCheckable)}"/&gt; &lt;Setter Property="MenuItem.IsChecked" Value="{Binding Path=(contracts:IMenuItem.IsChecked)}"/&gt; &lt;Setter Property="MenuItem.Command" Value="{Binding}"/&gt; &lt;Setter Property="MenuItem.Visibility" Value="{Binding Path=(contracts:IControl.Visible), Converter={StaticResource BooleanToVisibilityConverter}}"/&gt; &lt;Setter Property="MenuItem.ToolTip" Value="{Binding Path=(contracts:IControl.ToolTip)}"/&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=(contracts:IMenuItem.IsSeparator)}" Value="true"&gt; &lt;Setter Property="MenuItem.Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type MenuItem}"&gt; &lt;Separator Style="{DynamicResource {x:Static MenuItem.SeparatorStyleKey}}"/&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Menu.ItemContainerStyle&gt; &lt;/Menu&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload