0

I have application with toolbar (Add and Delete commands) and TabControl. There is VariableGrid control in each tabItem of TabControl.

look image at: http://trueimages.ru/view/cNFyf

<DockPanel >
    <ToolBarTray DockPanel.Dock="Top">
        <ToolBar>
            <Button Command="{x:Static VariableGrid:VariableGrid.AddRowCommand}"/>
            <Button Content="Delete" Command="ApplicationCommands.Delete" />
        </ToolBar>
    </ToolBarTray>

    <TabControl x:Name="tc">
        <TabItem Header="Tab 1">
            <vg:VariableGrid ItemsSource="{Binding Items1, Mode=TwoWay}"/>            </TabItem>
        <TabItem Header="Tab 2">
            <vg:VariableGrid ItemsSource="{Binding Items2, Mode=TwoWay}"/>
        </TabItem>
    </TabControl>
<DockPanel >

Toolbar commands are implemented in my control:

public partial class VariableGrid : DataGrid, INotifyPropertyChanged 
{
    public static RoutedCommand AddRowCommand = new RoutedCommand();
    public VariableGrid()
    {
        this.CommandBindings.Add(new CommandBinding(VariableGrid.AddRowCommand, AddRow)); 
        this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete, R    emoveRow, CanRemoveRow)); 
    }

    private void AddRow(object sender, ExecutedRoutedEventArgs e)
    {
        …
    }
    private void RemoveRow(object sender, ExecutedRoutedEventArgs e)
    {
        …
    }

    private void CanRemoveRow(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = (SelectedItems.Count > 0);
    }
}

There are few cases when commands in toolbar are disabled:

  1. when application is running
  2. when I click on gray field of DataGrid
  3. when DataGrid is empty

When any row of DataGrid is selected - commands of toolbar are becoming active.

Can you help me with my issue? What CommandTarget of toolbar buttons should I set?

PS: There are two VariableGrids in my application. Thats why I can't set CommandTarget as "{Binding ElementName=variableGrid}". I think it should be set to FocusedElement. But I don't know how to do this.

user1812267
  • 11
  • 1
  • 3

1 Answers1

0

WPF should be calling your CanRemoveRow method every once in a while to check if it is ok to remove a row. You should put a Boolean condition in this method that will answer that question. If you want similar functionality for your AddRowCommand, add a CanAddRow method where you bind the AddRowCommand.

You may want to read the Commanding Overview at MSDN.

UPDATE >>>

Oh... do you want to know what code to use for these disabled conditions? I'll assume so:

when application is running

I'm guessing you mean 'when application is busy'... add a Boolean property named IsBusy, set it to true when the application performs any long running processes, then add !IsBusy into your method condition.

when I click on gray field of DataGrid

when DataGrid is empty

Both of these conditions can be judged using the SelectedItem property of the DataGrid by adding && dataGrid.SelectedItem != null into your method condition.

Therefore, you want something like the following:

private void CanRemoveRow(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = !IsBusy && SelectedItem != null);
}
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • That's right. AddRowCommand has this default method, which always returns true. – user1812267 Jul 29 '13 at 12:12
  • CanRemoveRow method isn't called in these cases (1,2,3). – user1812267 Jul 29 '13 at 12:53
  • This is not suitable for me: I have several Grids and the problem is that I dont know how to bind command to an active grid – user1812267 Jul 29 '13 at 12:58
  • [This post](http://social.msdn.microsoft.com/Forums/vstudio/en-US/632ea875-a5b8-4d47-85b3-b30f28e0b827/mousedouble-click-or-singleclick-on-datagrid-in-mvvm-how-can-i-do-that) should help you with that. – Sheridan Jul 29 '13 at 13:03