开发者

commandbinding not working?

I working on a multi-tab application (For Ex: Multi-Tab Text Editor), where each tabitem has its own content. And in contextmenu of tabitem, their is menuitem with a command, say SelectAll command.

After running app, the menu item is always disabled, no command execution is done.

So, how can i make my commandbindings work ?

CODE ::

In Context Menu At TextEditor>

<MenuItem Command="local:TextEditor.SelectAllCommand" Header="Select All" />

In CommandBindings At TextEditor>

<UserControl.CommandBindings>
  <CommandBinding Command="local:TextEditor.SelectAllCommand" 
                  Executed开发者_C百科="SelectAll_Executed" CanExecute="SelectAll_CanExecute" />
</UserControl.CommandBindings>

The TabItems with TextEditor are created at run time


This happens since the ContextMenus are separate windows with their own VisualTree and LogicalTree.

Use like this

<MenuItem Header="Cut" Command="Cut" CommandTarget="
          {Binding Path=PlacementTarget, 
          RelativeSource={RelativeSource FindAncestor, 
          AncestorType={x:Type ContextMenu}}}"/>

For more check the link below

http://www.wpftutorial.net/RoutedCommandsInContextMenu.html


see biju answer, your DataContext for your ContextMenu is not the one you expect.

and if you have any binding problems in future, take a look at Snoop. its an easy to use tool to check your bindings at runtime.

i always check 2 things:

  • is my DataContext the one i expect?!
  • is my Binding Path the one i want?!
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜