我遇到了一个问题,无法呈现动态菜单的图标,该菜单使用视图模型作为ItemsSource。
我使用的解决方案在这里概述:
MVVM Dynamic Menu UI from binding with ViewModel
基本布局如下:
<Grid>
<Grid.Resources>
<HierarchicalDataTemplate DataType="{x:Type ViewModels:HeaderedItemViewModel}"
ItemsSource="{Binding Path=Children}">
<ContentPresenter RecognizesAccessKey="True"></ContentPresenter>
</HierarchicalDataTemplate>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Header" Value="{Binding Path=Header}" />
<Setter Property="InputGestureText" Value="{Binding Path=InputGestureText}" />
<Setter Property="Command" Value="{Binding Path=Command}" />
<Setter Property="Icon">
<Setter.Value>
<Image Source="{Binding Path=Icon}" Height="16px" Width="16px" />
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Menu Grid.Row="0" ItemsSource="{Binding Path=Shell.Navigation.Menus}" />
</Grid>
在上述样式中,绑定的“Icon”是“ImageSource”。设置如下。 BitmapImage image = null;
if (!string.IsNullOrEmpty(imagePath))
{
image = new BitmapImage(new Uri(imagePath, UriKind.Relative));
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
}
var menu = new HeaderedItemViewModel
{
Header = header,
InputGestureText = inputGesture,
ImagePath = imagePath,
Icon = image,
Command = command,
IsEnabled = isEnabled
};
我遇到的问题是与图标有关的。好像一次只能呈现一个图标?这就是我的意思。
打开下拉菜单...
只要另一个图像被呈现,第一个图像就会消失吗?换句话说,只有最后一个图像可见。这在菜单中的所有图像中都会发生。有什么想法吗?