WPF Commands and Command Target

| No Comments | No TrackBacks

So, had an interesting problem today. We are using a WPF DataGridView in our application and where having problems with the main menu of the window getting all its elements disabled whenever the datagrid was in row or cell edit mode.

At first we thought it had something to do with our 'single click checkbox in grid'-code, but it turned out it had something to do with command targets and how command targets are used to enable/disable commands.

The default command target is the currently active control in the window (the control with focus) - in our case the data grid being edited. The behaviour of a datagrid in edit state, is to disable all commands connected to it. Therefore are commands (menus) where getting disabled whenever the datagrid was in edit mode.

The fix is always simple once you know the problem - in our case - set the command target to something else that does not cause the command to be disabled. We changed it to the main window using the following code;

<MenuItem x:Name="mnuClose" Header="E_xit"Command="ApplicationCommands.Close"

    CommandTarget="{Binding ElementName=MainWindow}"

    InputGestureText="Alt-F4"></MenuItem>

There is one more gotcha with the above - make sure you commit all changes to the window to the model before actually handeling the exit, as if you do not, you may loose the last edit the user did - which is bad.

Cheers!

No TrackBacks

TrackBack URL: http://www.vc2go.com/cgi-sys/cgiwrap/webprog/managed-mt/mt-tb.cgi/3

Leave a comment

About this Entry

This page contains a single entry by Thies Schrader published on November 17, 2009 8:05 AM.

Cookie based authentication does not work with Excel was the previous entry in this blog.

errorMSB6006: "ResGen.exe" exited with code -1073741701. is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.21-en