Add Icon from C # code mahapps

时间:2016-07-11 19:21:23

标签: c# .net wpf xaml mahapps.metro

How can I add an mahapps icon from the code-behind in C#?

In the XAML, according to the doccumentation, it's done like this:

<Rectangle>
  <Rectangle.Fill>
     <VisualBrush Visual="{StaticResource appbar_add}" />
  </Rectangle.Fill>
</Rectangle>

But I have to do it in the code-behind.

Here's what I've tried:

Code-behind

MenuItem item = new MenuItem { Header = menu.Nombre };
var icono = new VisualBrush() { Visual = (Visual)Resources["appbar_check"] };

if (menu.Submenu != null)
{
   foreach (var subMenu in menu.Submenu)
   {
      MenuItem subItem = new MenuItem { Header = subMenu.Nombre };
      subItem.Click += Item_Click;
      item.Items.Add(subItem);
   }
}
item.Click += Item_Click;
MenuPrincipal.Items.Add(item);

XAML

<Menu Name="MenuPrincipal"></Menu>

1 个答案:

答案 0 :(得分:0)

它成功了

var rd = new ResourceDictionary
{
   Source = new Uri("/Laboratorio;component/Resources/Icons.xaml",
                        UriKind.RelativeOrAbsolute)
};

item.Icon = rd["appbar_clock"];