T.Poe
T.Poe

Reputation: 2089

WPF SelectedItem passed as a command parameter doesn't return value

<ScrollViewer VerticalScrollBarVisibility="Auto">
        <ListBox x:Name="RootListView" ItemsSource="{Binding Ratings}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid ShowGridLines="True">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="40" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>     

                         .... Some TextBoxes with Labels are here to fill the grid .....

                        <Button  Grid.Row="4" Grid.Column="1" Content="Delete" 
                                 Command="{Binding ElementName=RootListView, Path=DataContext.DeleteRatingCommand}"
                                 CommandParameter="{Binding ElementName=RootListView, Path=SelectedItem}">

                        </Button>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
 </ScrollViewer>

Where Ratings in my view model is:

public ObservableCollection<RatingModel> Ratings;

And the DeleteRatingCommand is an ICommand object.

There is a whole collection of ratings displayed in this view (a grid containing information), every one has a delete button. When the button of the corresponding rating is pressed I want to send the rating object to the view model with the SelectedItem property. Although the binded method is called, there is nothing passed with the SelectedItem parameter, a null value specifically. How can I use SelectedItem properly in this case to send info to the view model about which rating is to be deleted? Are there any other possibilities? I'm not sure if this approach is correct.

Upvotes: 1

Views: 1710

Answers (1)

b p
b p

Reputation: 86

I'm assuming that clicking the button doesn't actually set the SelectedItem property.

Try changing the binding to the row context data.

CommandParameter="{Binding Path=.}"

Upvotes: 2

Related Questions