In my DataContext I'm setting a BitmapImage e.g.
Image = new BitmapImage(uri);
In my style I have
<Setter Property="Icon" Value="{Binding Image, Mode=OneWay}" />
However the menuItem shows the uri as a string where the icon should be. Any idea what I'm missing
CodePudding user response:
The Icon
property does not directly accept an ImageSource
value.
Use an Image
element which takes the ImageSource as its Source
, and make sure to set x:Shared="false"
on the Style to allow for multiple instances of the Image element.
<Style TargetType="MenuItem" x:Shared="false">
<Setter Property="Icon">
<Setter.Value>
<Image Source="{Binding Image}"/>
</Setter.Value>
</Setter>
</Style>
CodePudding user response:
Not much of an answer but I was able to work around this using the Loaded event and setting the the Icons image in the handler.
void MenuItem_OnLoaded(object sender, RoutedEventArgs e)
{
var menuItem = (MenuItem)sender;
if (menuItem.DataContext is OptionMenuItemViewModel x)
menuItem.Icon = new Image { Source = new BitmapImage(x.ImageUri) };
}
With the following XMAL
<MenuItem Header="_Database" ItemsSource="{Binding DataBaseMenuItemViewModels}" >
<MenuItem.ItemContainerStyle >
<Style TargetType="MenuItem" >
<EventSetter Event="Loaded" Handler="MenuItem_OnLoaded"/>
<Setter Property="Header" Value="{Binding Path=Title}" />
<Setter Property="ToolTip" Value="{Binding Path=ToolTip}" />
<Setter Property="Command" Value="{Binding Path=Command}" />
<Setter Property="CommandParameter" Value="{Binding Path=Message}" />
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
However, I sure would like to know why the obvious didn't work.