简体   繁体   English

WPF中的ContextMenu太宽泛了

[英]ContextMenu in WPF is too wide

I've got a problem with my ContextMenu in WPF. 我在WPF中遇到了ContextMenu的问题。 The menu is far too wide- it's the width of the items I put on it, plus about fifty-a hundred pixels. 菜单太宽了 - 这是我放在它上面的物品的宽度,再加上大约五百个像素。 So when you open the menu, instead of being a clean list of options or buttons, there's loads of greyspace on each side. 所以当你打开菜单时,不是一个干净的选项或按钮列表,每边都有大量的greyspace。 How can I fix this? 我怎样才能解决这个问题?

Edit: Here's my XAML for the menu: 编辑:这是我的XAML菜单:

<ContextMenu Padding="0">
    <Button Content="Close Tab" Height="23" Name="closetabbutton" Width="75" Margin="0,0,0,0" Click="closetabbutton_Click" />
    <TextBox Height="23" Name="renamebox" Width="75" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ClipToBounds="True" TextChanged="renamebox_TextChanged" />
    <Button Content="Close Menu" Height="23" Name="closemenubutton" Width="75" Margin="0,0,0,0" Click="closemenubutton_Click" />
</ContextMenu>

The space is reserved for icons on the left, and input gesture text (eg Ctrl+C) on the right. 该空间保留在左侧的图标中,并在右侧输入手势文本(例如Ctrl + C)。 This is by design. 这是设计的。

If you wish to change this, you'll have to create your own ContextMenu style. 如果您想更改此设置,则必须创建自己的ContextMenu样式。 Here's an example of how to do this: 以下是如何执行此操作的示例:

http://www.dev102.com/2008/06/20/how-to-create-a-wpf-custom-context-menu/ http://www.dev102.com/2008/06/20/how-to-create-a-wpf-custom-context-menu/

Update 更新

Further to my question comment, MenuItems would normally be used where you have used buttons. 除了我的问题评论,MenuItems通常会在您使用按钮的地方使用。 For example: 例如:

<Grid.ContextMenu>
    <ContextMenu>
        <MenuItem Name="mnuClose" Header="Close tab" InputGestureText="Ctrl+C" />
        <MenuItem Name="mnuRename">
            <MenuItem.Header>
                <TextBox Name="txtRename" Width="100" />
            </MenuItem.Header>
        </MenuItem>
    </ContextMenu>
</Grid.ContextMenu>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM