locked
Binding current listview item to button commandparameter RRS feed

  • Question

  • I have the following XAML:

    <Button x:Name="ping"
            Style="{StaticResource AppBarButtonStyle}"
            IsEnabled="true"
            AutomationProperties.Name="Ping"
            Command="{Binding PingCommand}"
            CommandParameter="{Binding SelectedItem, ElementName=itemListView}">&#x1F4E1;</Button>

    I my C# code-behind, in the PingCommand, the 'parameter' value of the "CanExecute(object parameter)" is null.  However, if I manually put a string constant for CommandParameter, the value is passed correctly.

    Is there a way to get the SelectedItem object passed?

    Thursday, September 12, 2013 5:07 PM

All replies

  • What is the SelectionMode on the "itemsListView" ? Above should work if it is set to "Single"

    Maybe you can also inspect what is type of Object as "Selected" by attaching the SelectionChanged event handler.

    -Sagar

    Thursday, September 12, 2013 5:24 PM
  • Thanks for your response Sagar.

    Yes, the SelectionMode is 'Single'.  I've converted over from using a SelectionChanged event handler approach to Command based; therefore, I know the type of the selected item.

    From all the experimentation I've done up to this point, it is my belief that the CommandParameter in XAML does not support binding; that explains why only a simple string works.

    Workaround:  I've decided to pass the ListView object on the constructor of the Command and store it locally in that object; so that way, I can access the SelectedItem in the CanExecute and Execute methods.

    That's the best I can do for now.  If there's a better way, please let mw know.

    Thursday, September 12, 2013 9:44 PM
  • Hi,

    Could you please pass on a sample project to me for testing?

    -Sagar

    Monday, September 16, 2013 9:18 AM