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 ImageNa开发者_Python百科me}" />
<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?
In the click event use:-
ManageImageList.SelectedItem = ((Button)sender).DataContext;
精彩评论