Stretch ListBox项目命中区域到ListBox的全宽?ListBox样式通过主题设置为implicity

Nic*_*las 12 .net wpf xaml listbox listboxitem

我已经四处寻找答案,但潜在的重复更关注的是演示而不是互动.

我有一个基本的列表框,每个项目的内容都是一个简单的字符串.

ListBox本身被拉伸以填充它的网格容器,但每个ListBoxItem的hitarea不会镜像ListBox宽度.看起来每个项目的hitarea(指针接触区域)只是文本内容的宽度.无论文本大小如何,如何将这种拉伸完全展开.

我已将Horizo​​ntalContentAlignment设置为Stretch,但这并不能解决我的问题.我唯一的另一个猜测是内容实际上是拉伸,但背景是不可见的,所以不捕捉鼠标指针.

<ListBox 
    Grid.Row="1"
    x:Name="ProjectsListBox" 
    DisplayMemberPath="Name"
    ItemsSource="{Binding Path=Projects}" 
    SelectedItem="{Binding Path=SelectedProject}"
    HorizontalContentAlignment="Stretch"/>
Run Code Online (Sandbox Code Playgroud)

XAML非常直接.如果我将鼠标悬停在其中一个项目中的文本上,则该项目的整个宽度将变为活动状态.我想我只需要知道如何创建一个看不见的交互式背景.

Fre*_*lad 24

HorizontalContentAlignment="Stretch" 应该在ItemContainerStyle中设置为此工作.

Xaml示例

<ListBox xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <ListBox.ItemsSource>
        <x:Array Type="{x:Type sys:String}">
            <sys:String>String 1</sys:String>
            <sys:String>String 2</sys:String>
            <sys:String>String 3</sys:String>
            <sys:String>String 4</sys:String>
        </x:Array>
    </ListBox.ItemsSource>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" Background="Green"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

更新
试试这个

<ListBox ...>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)