Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
345
Bind the delete and the escape input keys of the XamDataTree item with view model commands
posted

Hello,

I am using the XamDataTree with the MVVM pattern. What I am trying to achieve is:

1) To trigger the delete view model command when you select an item and press the Delete button.

2) When you are in edit mode and press Escape to trigger the escape view model command and exit edit mode.

As you can see in the sample the ViewModelItem class contains the commands. When you select an item and press the Delete key ViewModelItem.DeleteCommand doesn't trigger (although it works with the delete button) and when you are in edit mode and press the Escape key the command is triggered but the item stays in edit mode.

XamTreeViewSample.zip
  • 34430
    Verified Answer
    Offline posted

    Hello George,

    Thank you for your post.

    I have been investigating into these issues that you are seeing with the XamDataTree, and it appears that both of the issues you are seeing are actually expected behavior of the control, but there are two diverse behaviors happening here.

    First, allow me to say that there is nothing wrong with the binding that you are placing on your KeyBinding objects that you are placing in the Item and Editor templates of your NodeLayout. The issue here has to do with where certain key presses are handled, or whether they are already handled. In this case, the Delete key is already handled, and you are handling the Escape key with your command.

    For the Delete key binding, the press of a Delete key is already handled in the source code of the control by the XamDataTreeNode class. There is node deletion functionality already built into the XamDataTree, which is what handles this. By setting an ItemTemplate for your nodes, you are essentially styling the template of the XamDataTreeNodeControl that "presents" your nodes. This XamDataTreeNodeControl element appears to be at a lower logical scope than the XamDataTreeNode that is handling the Delete key press. For this reason, the XamDataTree will never get to the KeyBinding placed on the ItemTemplate, as the KeyBindings look for an unhandled press of the Key desired.

    The exact opposite is happening with your Escape command. Normally, the Escape key handling would happen on the TextBox that you have placed in the EditorTemplate of your NodeLayout, but you are currently hooking a KeyBinding that looks for Escape on the Grid that is the parent element of your template. This grid is at a higher scope, and so when it hits this KeyBinding, the Escape key press notification never actually happens on the TextBox, as your custom escape command is overwriting it. Currently, all that is done in the escape command is the showing of a message box, and so the node has no logic to exit edit mode.

    To work around the Delete issue, I don't believe you will be able to do this on an item-basis through commanding with the current setup of the source code of the XamDataTreeNode elements. Instead, I would recommend writing a Behavior for the XamDataTree, as this will be a higher scope than your nodes. In this behavior, you can hook into the XamDataTree.PreviewKeyDown event and check for the Delete key. Then, you can check the XamDataTree.ActiveNode property to get the XamDataTreeNode. These nodes have a Data property, which you can use to get your ViewModelItem that the node represents. You also may want to make sure that the ActiveNode.IsEditing property is false, otherwise you will fire the Delete key logic while in edit mode.

    To work around the Escape key issue, I would simply recommend that you set a CommandParameter on the KeyBinding that you are currently using for the Escape command. This will pass a parameter to your command. If you bind directly to the DataContext of the KeyBinding, you will receive a XamDataTreeNodeDataContext object in your parameter for your command. This will allow you to get the node, the node control, and the XamDataTree that they belong to. You can then call the XamDataTree.ExitEditMode method to ensure that your nodes exit edit mode on the Escape key press.

    I have attached a modified version of the sample project you sent to demonstrate the above.

    I hope this helps you. Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Associate Developer
    Infragistics Inc.
    www.infragistics.com/support

    XamTreeViewSample.zip