【问题标题】:WPF Context Menu with dropdown list showing hyperlinks带有显示超链接的下拉列表的 WPF 上下文菜单
【发布时间】:2013-08-30 02:49:09
【问题描述】:

我知道如何使用 C# 中的代码在 xaml 中创建上下文菜单。我找不到的是如何拥有一个菜单项,单击该菜单项将显示一个列表,列表中的每个项目都是指向其他网站的超链接。任何帮助将不胜感激。 谢谢!

【问题讨论】:

    标签: wpf


    【解决方案1】:

    这就是您可以完全通过 MVVM 实现此目的的方法。

    XAML:

        <DataTemplate x:Key="SubMenuItemTemplate">
            <ContentControl>
                <Hyperlink>
                    <TextBlock Text="{Binding}"/> //Here you can bind to your site address property
                </Hyperlink>
            </ContentControl>
        </DataTemplate>
    
        <Style x:Key="MenuItemStyle" TargetType="{x:Type MenuItem}">
            <Setter Property="ItemsSource" Value="{Binding SubMenuItems}"/>
            <Setter Property="ItemTemplate" Value="{StaticResource SubMenuItemTemplate}"/>
        </Style>
    
        <ContextMenu x:Key="myContextMenu" ItemsSource="{Binding ContextMenuItems}" DisplayMemberPath="Name" ItemContainerStyle="{StaticResource MenuItemStyle}"> 
    

    您可以将任何控件的ContextMenu 属性设置为 myContextMenu 以使用它。 C#类

    public class ContextMenuItem
    {
        public string Name
        { get; set; }
    
        public List<String> SubMenuItems
        { get; set; }
    }
    

    ViewModel 可以具有属性 ContextMenuItems,即 ContextMenuItems 的集合。

    希望这会有所帮助

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多