Silverlight:单击子按钮时将焦点放在父ListBox项上

时间:2010-09-30 13:15:15

标签: c# silverlight xaml

我有以下XAML代码,删除了样式和格式标记:

<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>

ListBox绑定到ObservableCollection。我想在单击按钮时将焦点放在父ListBox项上,使其成为ListBox的SelectedItem。我该怎么做?

1 个答案:

答案 0 :(得分:2)

在点击事件中使用: -

 ManageImageList.SelectedItem = ((Button)sender).DataContext;