1

I have the following ListBox with an ItemTemplate

<ListBox SelectionMode="Multiple" ItemsSource="{Binding MyItems}" DisplayMemberPath="Name">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid VerticalAlignment="Stretch" Height="{Binding ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
                <ContentControl VerticalAlignment="Center" VerticalContentAlignment="Center" Content="{Binding}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

This displays all well and good, but the hit test on the ContentControl isn't right...if I click above or below the "actual" content (if the item has more Height than the ContentControl, then the item isn't selected.

If I change the ControlControl VerticalAlignment to Stretch, that resolves the problem and all clicks correctly highlight items...but then my Content is top-aligned within each item...

How can I get items to select correctly?

4

1 回答 1

3

Make the Grid hit-test for example

<Grid Background="Transparent" ...>
于 2012-08-23T20:52:04.003 回答