1

私は ItemTemplate を持つ次の ListBox を持っています

<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>

これはすべてうまく表示されますが、ContentControl のヒット テストは正しくありません...「実際の」コンテンツの上または下をクリックすると (項目の高さが ContentControl よりも大きい場合、項目は選択されません) .

ControlControl VerticalAlignment を Stretch に変更すると、問題が解決し、すべてのクリックでアイテムが正しく強調表示されます...しかし、コンテンツは各アイテム内で上揃えになります...

アイテムを正しく選択するにはどうすればよいですか?

4

1 に答える 1

3

Gridたとえば、ヒットテストを行います

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