单击空格不会触发列表框中的事件



我创建了一个ListBox,里面有TextBlock个,每个都有一个MouseLeftButtonUp事件。ListBox的大小比文本宽,当我单击ListBox中文本旁边的空白区域时,事件不会触发。它仍然选择该项目,用蓝色覆盖它,但在我单击文本之前不会触发该事件。

<ListBox Name="myListBox" Grid.Row="1" Margin="10, 0, 10, 10">
<TextBlock Name="X2" Text="X2, Power Head" Margin="0, 3, 0, 0" MouseLeftButtonUp="Selected_X2" />
<TextBlock Name="UB2X" Text="UB2X, Cab Car" Margin="0, 3, 0, 0" MouseLeftButtonUp="Selected_UB2X" />
<TextBlock Name="UA2" Text="UA2, 1st Class" Margin="0, 3, 0, 0" MouseLeftButtonUp="Selected_UA2" />
<TextBlock Name="UB2" Text="UB2, 2nd Class" Margin="0, 3, 0, 0" MouseLeftButtonUp="Selected_UB2" />
<TextBlock Name="URB2" Text="URB2, Bistro" Margin="0, 3, 0, 0" MouseLeftButtonUp="Selected_URB2" />
<TextBlock Name="URB2A" Text="URB2A, Bistro A" Margin="0, 3, 0, 0" MouseLeftButtonUp="Selected_URB2A" />
</ListBox>

设置一个ItemContainerStyle以将ListBoxItem内容(您的TextBlock)拉伸到全宽。

<ListBox Name="myListBox" Grid.Row="1" Margin="10, 0, 10, 10">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
<TextBlock Name="X2" Text="X2, Power Head" Margin="0, 3, 0, 0" MouseLeftButtonUp="Selected_X2" />
<TextBlock Name="UB2X" Text="UB2X, Cab Car" Margin="0, 3, 0, 0" MouseLeftButtonUp="Selected_UB2X" />
<TextBlock Name="UA2" Text="UA2, 1st Class" Margin="0, 3, 0, 0" MouseLeftButtonUp="Selected_UA2" />
<TextBlock Name="UB2" Text="UB2, 2nd Class" Margin="0, 3, 0, 0" MouseLeftButtonUp="Selected_UB2" />
<TextBlock Name="URB2" Text="URB2, Bistro" Margin="0, 3, 0, 0" MouseLeftButtonUp="Selected_URB2" />
<TextBlock Name="URB2A" Text="URB2A, Bistro A" Margin="0, 3, 0, 0" MouseLeftButtonUp="Selected_URB2A" />
</ListBox>

最新更新