可见但不可选的 XAML 菜单项? [英] XAML menuitem that is visible but not selectable?
问题描述
所以我对 XAML 非常陌生并且仍在学习.我已经学习了几个关于菜单和菜单项的教程,但我似乎无法找到一种方法来向菜单添加某些内容并且无法选择它.我尝试只添加一个标签,它仍然显示为可选.我尝试将 isenabled 设置为 false 并且它确实变成了灰色,但仍然可以选择.我尝试关闭hittest,但也没有用.我的猜测是这与设置自定义样式有关,我对此一无所知.我将返回谷歌并深入研究,但同时我想在这里发帖,看看是否有人可以提供有关前进方向的见解.
So I'm extremely new to XAML and still learning. I've followed several tutorials on the menu and menuitems but I cannot seem to find a way to add something to a menu and not have it be selectable. I tried adding just a label and it still shows as selectable. I tried setting isenabled to false and it did turn grey, but was still selectable. I tried turning off hittest and that didn't work either. My guess is that this has something to do with setting up a custom style, which I know nothing about. I'm going to head back into the googles and dive into that, but in the mean time I wanted to post here to see if anyone could offer insight as to a good direction to head.
我试过的简单例子:
<Menu>
<MenuItem Header="My Menu">
<Label Content="Label1"/> //Selectable even though it's a label. I would Expect a label to not be selectable.
<MenuItem Header="Menu Item 1"/> //Selectable as it should be.
<Separator /> //Not selectable
<MenuItem Header="Menu Item 2"/>
<Label Content="Another Label" IsEnabled="False"/> //Selectable even though it is set to IsEnabled=False.
</MenuItem>
</Menu>
推荐答案
这可能更像您想要的那样.我添加了 Focusable="False"
和 IsHitTestVisible="False"
.请注意,我不得不将最后一个 更改为
因为
Focusable="False"
并没有完全做到那里.
This may work a bit more like what you want. I added Focusable="False"
and IsHitTestVisible="False"
. Note I had to change the last <Label>
to <MenuItem>
since Focusable="False"
didn't quite do it there.
<Menu>
<MenuItem Header="My Menu">
<Label Content="Label1"/>
<MenuItem Header="Menu Item 1" Focusable="False" IsHitTestVisible="False"/>
<Separator />
<MenuItem Header="Menu Item 2"/>
<MenuItem Header="Another Label" IsEnabled="False" Focusable="False" IsHitTestVisible="False"/>
</MenuItem>
</Menu>
这篇关于可见但不可选的 XAML 菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!