相关疑难解决方法(0)

与ViewModel绑定的MVVM动态菜单UI

我正在与一个关于LoB应用程序的团队合作.我们希望有一个动态Menu控件,它根据登录的用户配置文件创建菜单.在以前的开发方案(即ASP.NET)中,我们使用迭代描述集合和MenuItem动态生成的数据.在MVVM中我该怎么做?我可以将XAML视图与描述菜单元素的ViewModel分开吗?

解:

通过评论员的输入,我能够Menu动态绑定ViewModel中的数据.这篇文章也很有帮助.

XAML:

<HierarchicalDataTemplate DataType="{x:Type self:Menu}" ItemsSource="{Binding Path=Children, UpdateSourceTrigger=PropertyChanged}">
    <ContentPresenter Content="{Binding Path=MenuText}" RecognizesAccessKey="True"/>
</HierarchicalDataTemplate>

[...]

<Menu Height="21" Margin="0" Name="mainMenu" VerticalAlignment="Top" HorizontalAlignment="Stretch" 
      ItemsSource="{Binding Path=MenuItems, UpdateSourceTrigger=PropertyChanged}" ItemContainerStyle="{StaticResource TopMenuItems}">
    <Menu.Background>
        <ImageBrush ImageSource="/Wpf.Modules;component/Images/MenuBg.jpg" />
    </Menu.Background>
</Menu>
Run Code Online (Sandbox Code Playgroud)

Menu 数据类:

public class Menu : ViewModelBase
{
    public Menu()
    {
        IsEnabled = true;
        Children = new List<Menu>();
    }

    #region [ Menu Properties ]

    private bool _isEnabled;
    private string _menuText;
    private ICommand _command;
    private IList<Menu> _children;

    public string MenuText …
Run Code Online (Sandbox Code Playgroud)

c# wpf menu mvvm

16
推荐指数
3
解决办法
4万
查看次数

WPF - MenuItem缺少图标/图像

我得到menuItem图标只出现在最后一个menuItem上.如果我只是窥探应用程序,最后一个menuItem在图标中有图像,而如果我调试所有MenuItems似乎在图标中有图像.此外,如果我添加submenuItem一旦我打开子菜单,menuItem上的图标消失,最后一个子菜单获取图标...任何想法?PS:菜单项上的工具提示也不起作用.我使用caliburn微型和流畅的色带控制.

        <ControlTemplate x:Key="dropDownButton">
        <ef:DropDownButton Header="{Binding DisplayName}" 
                           ItemsSource="{Binding Items}" 
                           LargeIcon="{Binding LargeIconPath}" 
                           cm:Message.Attach="ClickAction()" 
                           ef:KeyTip.Keys="{Binding KeyTip}">
            <ef:DropDownButton.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="Header" 
                            Value="{Binding DisplayName}"/>
                    <Setter Property="Icon">
                        <Setter.Value>
                            <Image Source="{Binding Path=IconPath}"/>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="ItemsSource" 
                            Value="{Binding Items}"/>
                    <Setter Property="cm:Message.Attach" 
                            Value="ClickAction()"/>
                    <Setter Property="ef:KeyTip.Keys" 
                            Value="{Binding KeyTip}"/>
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <ef:ScreenTip Title="{Binding DisplayName}"
                                          HelpTopic="ScreenTip help ..."
                                          Image="{Binding LargeIconPath}"
                                          Text="Text for ScreenTip"/>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ef:DropDownButton.ItemContainerStyle>
            <ef:DropDownButton.ToolTip>
                <ef:ScreenTip Title="{Binding DisplayName}"
                              HelpTopic="ScreenTip help ..."
                              Image="{Binding LargeIconPath}"
                              Text="Text for ScreenTip"/>
            </ef:DropDownButton.ToolTip>
        </ef:DropDownButton>
Run Code Online (Sandbox Code Playgroud)

c# wpf icons visual-studio-2010 menuitem

6
推荐指数
1
解决办法
6268
查看次数

标签 统计

c# ×2

wpf ×2

icons ×1

menu ×1

menuitem ×1

mvvm ×1

visual-studio-2010 ×1