Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an example that sets the image path dynamically (image located somewhere on disc rather than build as resource):</p> <pre><code>if (File.Exists(imagePath)) { // Create image element to set as icon on the menu element Image icon = new Image(); BitmapImage bmImage = new BitmapImage(); bmImage.BeginInit(); bmImage.UriSource = new Uri(imagePath, UriKind.Absolute); bmImage.EndInit(); icon.Source = bmImage; icon.MaxWidth = 25; item.Icon = icon; } </code></pre> <h2>Reflections on Icons...</h2> <p>First thought, you would think that the Icon property can only contain an image. But it can actually contain anything! I discovered this by accident when I programmatically tried to set the <code>Image</code> property directly to a string with the path to an image. The result was that it did not show the image, but the actual text of the path!</p> <p>This leads to an alternative to not have to make an image for the icon, but use text with a symbol font instead to display a simple "icon". The following example uses the <a href="https://en.wikipedia.org/wiki/Wingdings" rel="nofollow noreferrer">Wingdings font</a> which contains a "floppydisk" symbol. This symbol is really the character <code>&lt;</code>, which has special meaning in XAML, so we have to use the encoded version <code>&amp;lt;</code> instead. This works like a dream! The following shows a floppydisk symbol as an icon on the menu item:</p> <pre><code>&lt;MenuItem Name="mnuFileSave" Header="Save" Command="ApplicationCommands.Save"&gt; &lt;MenuItem.Icon&gt; &lt;Label VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="Wingdings"&gt;&amp;lt;&lt;/Label&gt; &lt;/MenuItem.Icon&gt; &lt;/MenuItem&gt; </code></pre>
 

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