WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法
生活随笔
收集整理的這篇文章主要介紹了
WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
WPF中ContextMenu(右鍵菜單)使用Command在部分控件上默認為灰色的處理方法 原文:WPF中ContextMenu(右鍵菜單)使用Command在部分控件上默認為灰色的處理方法
問題描述
今天發現如果我想在一個TextBlock弄一個右鍵菜單,并且使用Command綁定,結果發現默認菜單式不可用的.
問題原因
這個問題不知道算不算BUG,反正如果是在一個不可獲得的焦點的東西上面使用ContextMenu,CanExecute的事件路由就會停止在ContextMenu,就沒辦法到達窗體上.一個解決的方案是吧CommandTarget設置到窗體上去.
事例代碼
<Window x:Class="TestPopupMenuCommand.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Window.CommandBindings><CommandBinding Command="Close" Executed="CloseCommand"/><CommandBinding Command="New" Executed="NewCommand"/></Window.CommandBindings><Grid><TextBlock><TextBlock.ContextMenu><ContextMenu><MenuItem Command="New" CommandTarget="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType=ContextMenu}}"/><MenuItem Command="Close"/></ContextMenu></TextBlock.ContextMenu>test</TextBlock></Grid> </Window>代碼分析
里面的兩個菜單項,New的是可用的,Close默認是不可以用的.因為我把New的CommandTarget修改了.
里面具體的command實現代碼我就不貼了.
posted on 2019-04-05 00:18 NET未來之路 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/lonelyxmas/p/10657682.html
總結
以上是生活随笔為你收集整理的WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TP3.2.x判断手机端访问并设置默认访
- 下一篇: 关于QueryRunner数据查询以及常