WPF Delete button in listview mvvm

c#, mvvm, wpf

Solution

That is because DataContext of button is ListBoxItem DataContext. So you need to go to parent ListView DataContext.

one way to do that, is to give ListView a name, and to bind with element name

<ListView Name="lv" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
        HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Grid.Row="2"
        ItemsSource="{Binding AssignedSubjects}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Center" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridViewColumn Width="140" Header="Subjects" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Width="auto">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="X" Command="{Binding ElementName=lv,Path=DataContext.RemoveSubjectCommand}"  />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

Problem

I have a button inside a listview to delete selected item.when i click on the button RemoveSubjectCommand is not firing. if i put the button outside the listview it is working fine. hopw this is just because of nested item. how can i solve this problem? ``` <ListView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Grid.Row="2" ItemsSource="{Binding AssignedSubjects}"> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="HorizontalContentAlignment" Value="Center" /> </Style> </ListView.ItemContainerStyle> <ListView.View> <GridView> <GridViewColumn Width="140" Header="Subjects" DisplayMemberBinding="{Binding Name}" /> <GridViewColumn Width="auto"> <GridViewColumn.CellTemplate> <DataTemplate> <Button Content="X" Command="{Binding RemoveSubjectCommand}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> ``` View Model, ``` private ICommand removeSubjectCommand; ** public ICommand RemoveSubjectCommand { get { return removeSubjectCommand ?? (removeSubjectCommand = new RelayCommand(param => this.RemoveSubject(), null)); } } ** private void RemoveSubject() { *** } ``` If i put following code, it will work fine. ``` <ListView.InputBindings> <KeyBinding Key="Delete" Command="{Binding RemoveSubjectCommand}" /> </ListView.InputBindings> ```

Original source