0

i have writte a custom ICommand implementation which is available as a static property:

public class GridViewCommands
{
    public GridViewCommands()
    {

    }

    /// <summary>
    /// Toggle Selection-Command
    /// </summary>
    public static ICommand ToggleSelection
    {
        get
        {
            return new GridViewToggleSelectionCommand();
        }
    }
}

I try to bind this property to a simple Button-Command

<ui:GridViewControl x:Name="gridView" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />

<Button HorizontalAlignment="Left" Width="100" Margin="220,0,0,0" Content="Toggle" x:Name="toggleButton" Command="{x:Static  ui:GridViewCommands.ToggleSelection}" CommandParameter="{Binding ElementName=gridView}"></Button>

But if i start my application, the parameter-Parameter in the CanExecute method in GridViewToggleSelectionCommand is always null. My aim is to pass an instance of GridViewControl as the command parameter.

What am i doing wrong here: ui:GridViewCommands.ToggleSelection}" CommandParameter="{Binding ElementName=gridView"}?

EDIT

The binding does not throw any exception.

Thank you very much!

BendEg
  • 20,098
  • 17
  • 57
  • 131
  • Did you check your bindings for errors? If not try to use Snoop to do so. Great app btw. – Kędrzu Aug 12 '15 at 15:09
  • If i turn exception throwing on or use this: http://stackoverflow.com/a/4226038/2630261 i don't get any exceptions. – BendEg Aug 12 '15 at 20:31

2 Answers2

0

I think you're trying to do this:

public static ICommand ToggleSelection { get { return new RelayCommand<GridViewControl>(GridViewToggleSelectionCommand); } }
private static void GridViewToggleSelectionCommand(GridViewControl theControl)
{
    // do something with the control here
}

I see you've tagged this as mvvm...this is NOT mvvm. Your view models shouldn't know anything about the views. I also can't for the life of me see why you're setting ToggleSelection as static, it means your command handler has to be static as well.

Honestly, you are setting yourself up for a world of pain if you continue down the path you are taking. Your GridViewControl should be binding to an object in your view model, and it's this object that you should be passing back to the view model via your ICommand handler.

Mark Feldman
  • 15,731
  • 3
  • 31
  • 58
  • I've tried to use the same command pattern as telerik does. My grid view has an `EcecuteCommand` method, which takes my command as a parameter. So the static stuff is only for easy to use. – BendEg Aug 14 '15 at 05:18
0

You actually can get the parameter when you click the button.

daniel
  • 1,010
  • 6
  • 15