代码之家  ›  专栏  ›  技术社区  ›  Hitesh Kansagara

如何减少wpf上下文菜单项的内容长度

wpf
  •  1
  • Hitesh Kansagara  · 技术社区  · 7 年前

    我正在学习WPF并创建类似窗口浏览器的面包屑。 但在设置面包屑上下文菜单项长度时面临着问题。我想修改菜单项50的字符长度,当字符长度超过50时,消除多余的字符并设置…最后如下图所示

    我的菜单项代码:

    <contextmenu>
    <contextmenu.itemtemplate>
    <层次结构数据模板>
    <menuitem header=“绑定用户名”
    fontfamily=“Arial”
    ftToSt= =“11”
    height=“22”margin=“-22,-20,-40,-20”
    command=“绑定PlacementTarget.DataContext.SelectObject,
    relativesource=relativesource mode=findancestor,
    ancestorType=x:类型contextmenu
    commandParameter=“绑定”style=“动态源菜单项样式”>
    <menuitem.tooltip>
    <tooltip content=“绑定用户名”>
    </tooltip>
    </menuitem.tooltip>
    </menuitem>
    </hierarchicaldatatemplate>
    </contextmenu.itemtemplate>
    </contextmenu>
    < /代码> 
    
    

    .

    我的菜单项代码:

        <ContextMenu>
       <ContextMenu.ItemTemplate>
        <HierarchicalDataTemplate>
            <MenuItem Header="{Binding UserName }"                                                     
                FontFamily="Arial"
                FontSize="11"
                Height="22" Margin="-22,-20,-40,-20"                                                
                Command="{Binding PlacementTarget.DataContext.SelectObject,
                RelativeSource={RelativeSource Mode=FindAncestor, 
                AncestorType={x:Type ContextMenu}}}" 
                CommandParameter="{Binding}" Style="{DynamicResource MenuItemStyle}">
            <MenuItem.ToolTip>
                <ToolTip Content="{Binding UserName}">
                </ToolTip>
            </MenuItem.ToolTip>
            </MenuItem>
        </HierarchicalDataTemplate>
       </ContextMenu.ItemTemplate>
    </ContextMenu>
    

    This is the image from windows explorer breadcrumb. that I want to make

    2 回复  |  直到 7 年前
        1
  •  1
  •   Coskun Ozogul    7 年前

    我认为最简单的方法是创建另一个要在标题上显示的属性:

        public string ShortUserName
        {
            get
            {
                return (UserName.Length > 50) ? UserName.Substring(0, 47) + "..." : UserName;
            }
        }
    
    
        <MenuItem Header="{Binding ShortUserName }" 
          Tag={Binding UserName} //If need, you can find the complete username on the Tag property of your menu item.                                                    
          FontFamily="Arial"...
    
        2
  •  1
  •   Hitesh Kansagara    7 年前

    我发现上下文菜单项标题内容宽度随…最后,当使用如下字符省略号控件从特定宽度超出时,

    <MenuItem FontSize="11" Height="22" Margin="-18,-20,-40,-20"                                                                                   
    Command="{Binding PlacementTarget.DataContext.CrumbSelectObject,
              RelativeSource={RelativeSource Mode=FindAncestor, 
              AncestorType={x:Type ContextMenu}}}" 
              CommandParameter="{Binding}">
    <!-- char ellipsis control -->
    <MenuItem.Header>
       <TextBlock MaxWidth="150" 
            TextTrimming="CharacterEllipsis"
            Text="{Binding UserName}" 
            FontFamily="Arial" 
            FontSize="11">
        </TextBlock>
    </MenuItem.Header>