3

I am using RelayCommand to handle a button click, I need to get the sender parameter but it is always null, any idea why?

ViewModel.cs

    private RelayCommand _expandClickCommand;
    public ICommand ExpandClickCommand
    {
        get
        {
            if (_expandClickCommand == null)
            {
                _expandClickCommand = new RelayCommand(ExpandClickCommandExecute, ExpandClickCommandCanExecute);
            }
            return _expandClickCommand;
        }
    }

    public void ExpandClickCommandExecute(object sender)
    {
        //sender is always null when i get here! 
    }
    public bool ExpandClickCommandCanExecute(object sender)
    {
        return true;
    }

View.xaml

<ListBox ItemsSource="{Binding Path=MyList}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Button Grid.Column="0" Grid.Row="0" Content="Expand" Command="{Binding DataContext.ExpandClickCommand,ElementName=SprintBacklog}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I need to get the index of the currect ListboxItem in ExpandClickCommand

Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83

1 Answers1

13

That object in all likelihood is not the sender but the CommandParameter that is passed by the control. You could bind the CommandParameter of the button to itself to immitate the sender.

CommandParameter="{Binding RelativeSource={RelativeSource Self}}"

(But that might not really help you that much, so think about what you pass in that helps you get that value.)

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Well I'm now getting something in the sender object but I cant seem to get the index, I changed that parameter to pass in the ListBox, but the CurrentItem property is always 0 – Eamonn McEvoy Oct 29 '11 at 22:46
  • @EamonnMcEvoy: What `CurrentItem` property? ListBoxes have no such property. What exactly is your command doing? Can't you get away with a XAML-only solution? – H.B. Oct 29 '11 at 22:52
  • @EamonnMcEvoy: Have you thought about using the `DataContext` (the current item) and getting the position from your data-list or is there some view on top of the collection so the list's `IndexOf` would produce wrong values? *(Even then you can probably manage to get your hands on that view and get the right index from it)* – H.B. Oct 29 '11 at 23:03
  • sorry I meant SelectedItem. My xaml uses a List of objects in my viewmodel to populate its ListBox, I have a corresponding list of Visibility objects in my viewmodel, when I click the button I want to make some details hidden. So I want the index of the SelectedItem so I can update the correct item in the Visibility list (its a messy solution I know, but I cant think of another way) – Eamonn McEvoy Oct 29 '11 at 23:07
  • 1
    @EamonnMcEvoy: Just use a dictionary instead, the key is the item, the value is the visibility, basically that's how dependency properties work as well. Using indices is messy indeed. – H.B. Oct 29 '11 at 23:11
  • @EamonnMcEvoy: Or you could change your architecture a bit, create a viewmodel for each item, the viewmodel then contains the data-item and a visibility property and you have a list of those viewmodels. – H.B. Oct 29 '11 at 23:17