views:

14

answers:

1

I have the following XAML code, having removed the styling and formatting tags:

<ListBox Name="ManageImageList">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Image Name="ManageImageThumbnail" Source="{Binding ImageName}" />
                <StackPanel Orientation="Vertical" >
                    <TextBlock Name="ManageImageUrl" Text="{Binding ImageName}" />
                    <TextBlock Name="ManageImageComment" Text="{Binding Comment}" />
                </StackPanel>
                <Button Name="ManageImageDelete" ClickMode="Press" Click="ManageImageDelete_Click" Content="X" />
           </StackPanel>
        </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

The ListBox is bound to an ObservableCollection. I would like to give focus to the parent ListBox item when the button is clicked, making it the SelectedItem of the ListBox. How do I do this?

+1  A: 

In the click event use:-

 ManageImageList.SelectedItem = ((Button)sender).DataContext;
AnthonyWJones
Works perfectly, thank you Anthony!
Scott