我有一个listbox在其中添加textblock和 listboxitem在每一行中。XML 看起来像这样:
<ListBox x:Name="DatabaseBox" ItemsSource="{Binding Book}">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanelOrientation="Horizontal" Width="auto" Height="22">
<Image x:Name="ToggleFavoriteImage" Width="10" Height="10" Tag="{Binding Tag}" Source="{Binding ImageSource}" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding Name}" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Center"/>
<ListBoxItem Content="{Binding City}" HorizontalAlignment="Center"/>
<ListBoxItem Content="{Binding Author}" HorizontalAlignment="Center"/>
<ListBoxItem Content="{Binding Country}" HorizontalAlignment="Center"/>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
我想要做的是Mouse Event为他们每个人提供一个并获取TextfromTextBlock和 the Contentfrom ListBoxItem。我设法得到TextBlock Text这样的:
StringBuilder SName = new StringBuilder();
var name = sender as System.Windows.Controls.TextBlock;
SName.Append(name.Text);
但是当我尝试做同样的 事情时,ListBoxItem什么也没发生。似乎我都无法选择其中任何一个。感觉前面有什么东西ListBoxItem。有任何想法吗?