【发布时间】:2018-02-22 10:17:25
【问题描述】:
我正在开发一个菜单栏,只使用Controls:Tiles 和属性ContextMenu,它只允许我点击鼠标右键打开它。经过一番搜索,我用鼠标左键点击它,但行为不同:当我按下右键时,ContextMenu 打开并且所有MenuItems 都是可点击的。当我按下左键时,ContextMenu 打开,但所有MenuItems 看起来都已禁用,我无法点击它们。
我的代码在这里:
<Controls:Tile x:Name="FileTile" Width="Auto" Height="30" TitleFontSize="10" HorizontalContentAlignment="Left"
VerticalAlignment="Top" VerticalContentAlignment="Top" Background="White" HorizontalTitleAlignment="Right"
Foreground="Gray" ContextMenuService.IsEnabled="True">
<Controls:Tile.Content>
<StackPanel Orientation="Horizontal" Height="Auto">
<Image Source="/images/topbaricons/files.png" Width="22" Height="22" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<TextBlock Text="{x:Static p:Resources.File}" VerticalAlignment="Center" Margin="5,0,5,0" />
</StackPanel>
</Controls:Tile.Content>
<Controls:Tile.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<EventTrigger RoutedEvent="Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen" FillBehavior="HoldEnd">
<DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Controls:Tile.Style>
<Controls:Tile.ContextMenu>
<ContextMenu x:Name="File_ContextMenu" >
<MenuItem Header="{x:Static p:Resources.Menu_item_New}" Command="ApplicationCommands.New" IsEnabled="true">
<MenuItem.Icon>
<Image Source="/images/topbaricons/new_file.png" Height="22" Width="22" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="{x:Static p:Resources.Menu_item_Open}" Command="ApplicationCommands.Open" IsEnabled="true">
<MenuItem.Icon>
<Image Source="/images/topbaricons/open_file.png" Height="22" Width="22" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="{x:Static p:Resources.Menu_item_Save}" x:Name="MenuItemSave" Command="ApplicationCommands.Save" IsEnabled="False">
<MenuItem.Icon>
<Image Source="/images/topbaricons/save.png" Height="22" Width="22" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="ApplicationCommands.SaveAs" Visibility="Visible" >
<MenuItem.Icon>
<Image Source="/images/topbaricons/save_as.png" Height="22" Width="22" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="{x:Static p:Resources.Menu_item_Exit}" Command="ApplicationCommands.Close" IsEnabled="true">
<MenuItem.Icon>
<Image Source="/images/topbaricons/off.png" Height="22" Width="22" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</Controls:Tile.ContextMenu>
</Controls:Tile>
为了准确展示我在这篇文章中所说的差异,有这张图片:
有人可以帮助我吗?我真的很想用左键单击鼠标右键。
谢谢。
【问题讨论】:
-
感谢@Elhamer 的编辑
-
你点击的是同一个地方吗?
-
@Rekshino 是的,我愿意!唯一的变化是点击左键或右键
-
你在哪里绑定你的命令?
-
我像这样绑定我的命令:
<Window.CommandBindings> <CommandBinding Command="New" Executed="NewCommand_Executed"></CommandBinding> <CommandBinding Command="Open" Executed="OpenCommand_Executed"></CommandBinding> <CommandBinding Command="Save" Executed="SaveCommand_Executed"></CommandBinding> <CommandBinding Command="SaveAs" Executed="SaveAsCommand_Executed"></CommandBinding> <CommandBinding Command="Close" Executed="CloseCommand_Executed"></CommandBinding> </Window.CommandBindings>
标签: wpf xaml contextmenu