Select whole item in ListBox in WPF [duplicate]
I've been googling around for something similar to FullRowSelect for a listview.开发者_如何转开发 Currently my listbox items are only selectable if you click on a part of it that is being taken up with content eg a textblock, item etc. This means that the "clickable" area is dependant on how long the entry is, but ideally i'd like to be able to click anywhere within the rectangle that defines each item.
I hope that makes sense
You need to adjust the ItemContainerStyle
within the ListBox
so that its HorizontalContentAlignment
is set as Stretch
versus Left
. You can still align the content within the ListBoxItem
as needed.
<Window.Resources>
<Style x:Key="Stretched" TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</Window.Resources>
<Grid>
<ListBox ItemContainerStyle="{StaticResource Stretched}">
<ListBox.Items>
<ListBoxItem>
<Grid>
<TextBox HorizontalAlignment="Left" Height="25" Width="100"/>
</Grid>
</ListBoxItem>
<ListBoxItem>
<Grid>
<TextBox HorizontalAlignment="Left" Height="25" Width="100"/>
</Grid>
</ListBoxItem>
</ListBox.Items>
</ListBox>
</Grid>
精彩评论