我有一个结合到通过我的数据模板显示每个项目的自定义类型的可观察集合列表框:我的列表框的WPF鼠标绑定的列表框项目模板不一致
<ListBox x:Name="ListBox" Style="{StaticResource CustomListBox}" ItemsSource="{Binding HandStats}" Height="410" Width="150" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding HoleCards[0].CardParts.Value}" Margin="2"/>
<Image Source="{Binding HoleCards[0].SuitImagePath}" Width="10" Height="10" Margin="2"/>
<TextBlock Text="{Binding HoleCards[1].CardParts.Value}" Margin="2"/>
<Image Source="{Binding HoleCards[1].SuitImagePath}" Width="10" Height="10" Margin="2"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Runs:" Margin="2"/>
<TextBlock Text="{Binding NumberOfRuns}" Margin="2"/>
<TextBlock Text="Players:" Margin="2"/>
<TextBlock Text="{Binding NumberOfPlayers}" Margin="2"/>
</StackPanel>
<StackPanel.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding Path=DataContext.PopulateReport, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
CommandParameter="{Binding ElementName=ListBox, Path=SelectedItem}"/>
</StackPanel.InputBindings>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
现在理想情况下,当我点击堆栈面板中每个项目的任何地方时,该项目将被选中,命令将被触发。但我的问题是,项目选择和命令触发不能正常工作。
只有当我点击文本块之间或文本块右侧的空白区域时,该项才会被选中,但命令不会被触发。该命令只会在我第一次选择该项目后触发,然后再次点击其中一个文本块或其中一个图像。
我还是很新的WPF,所以如果我不是做多大意义,请让我知道,在此先感谢:)
尝试这与外面的堆叠面板,然后内面板,但仍然没有运气。我现在用不同的方式解决我的问题了。我会用我的答案更新这篇文章 – rejy11