Setting MenuItem icon through style installer

<Style x:Key="ContextMenuItemStyle" TargetType="{x:Type MenuItem}">
    <Setter Property="Icon" Value="{Binding Icon}" />
    <Setter Property="Header" Value="{Binding Text}" />
    <Setter Property="ItemsSource" Value="{Binding Children}" />
    <Setter Property="Command" Value="{Binding Command}" />
</Style>

setting it into code as follows:

Uri refreshUri = new Uri("..\\Resources\\Refresh16.bmp",UriKind.Relative);
BitmapImage refreshIcon = new BitmapImage();
refreshIcon.UriSource = refreshUri;

Icon does not appear, any hints?

+5
source share
2 answers

If this refreshIconis the source of your property Icon, you may need to either invoke NotifyPropertyChanged("Icon")after your sample code (and implement the interface INotifyPropertyChanged) or declare Iconhow DependencyProperty.

Here is a link to more information about the interface INotifyPropertyChanged.

Ahh, I see your problem ... try setting the property Iconto Imageand bind to the source Image:

<Setter Property="Icon">
    <Setter.Value>
        <Image Source="{Binding Icon}" />
    </Setter.Value>
</Setter>

"" xaml :

<Setter Property="Icon">
    <Setter.Value>
        <Image Source="/ProjectName;component/Images/IconName.ico" />
    </Setter.Value>
</Setter>
+7

, , :

<Window.Resources>
    <Image x:Key="Icon" Source="/ProjectName;component/Images/IconName.ico" x:Shared="false"/>
    <Style x:Key="MenuItem">
        <Setter Property="MenuItem.Header" Value="Header Text"/>
        <Setter Property="MenuItem.Icon" Value="{DynamicResource Icon}"/>
    </Style>
</Window.Resources>
+4

All Articles