0

I have the following user control

<ListBox   ItemsSource="{Binding Persons}" 
           SelectedItem="{Binding SelectedPerson}" 
           VerticalAlignment="Top" Width="166" >
            <ListBox.Template>
                <ControlTemplate>
                    <StackPanel >
                        <ItemsPresenter/>
                        <Button Content="Add" Background="Transparent" Command="{Binding NewItemCommand}"/>
                    </StackPanel>
                </ControlTemplate>
            </ListBox.Template>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Button  Height="16" Width="16" Background="Transparent" Command="{Binding DeleteItemCommand}">
                            <Image Source="images/delete-icon.png" />
                        </Button>
                        <TextBlock Text="{Binding Name}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

and i have a view model for it with two commands, the first command that you can see above NewItemCommand is working fine, how ever the second command DeleteItemCommand doesn't function.

is it because its in the item template?

1 Answer 1

2

Yes, this is because the DataContext for the ItemTemplate is the Item from Persons not the ViewModel

To bind the DeleteItemCommand on each item you will need to bind back to the ViewModel that is holding the command

Example, binding to the DataContext of the ListBox

<Button Command="{Binding Path=DataContext.DeleteItemCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}" />

Edit:

If you want to remove the item that the button was clicked on you can pass the item the button belongs to as the CommandParameter and handle that in your comman, Not sure what type of command you are using but this is simple if you are using RelayCommand or DelegateCommand

 <Button Command="{Binding Path=DataContext.DeleteItemCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"
         CommandParameter="{Binding}" />

public MainWindow()
{
    InitializeComponent();
    DeleteItemCommand = new RelayCommand(person => DeletePerson(person as Person));
}

private void DeletePerson(Person person)
{
    Collection.Remove(person);
}
Sign up to request clarification or add additional context in comments.

2 Comments

perfect thanks, any idea on how would you remove an item from the collection? my delete command deletes the selected item , however the item doesnt get the focused if i click its remove button without focusing it first
You could pass the Item as the CommandParameter and remove it from the collection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.