弹力列表框项目灾区到ListBox的整个宽度?列表框样式是通过一个主题设置隐含性列表、灾区、弹力、宽度

由网友(一桶浆糊要一统江湖)分享简介:我环顾四周,在这一个答案,但潜在的重复更关心的是presentation不是交互。 I've looked around for an answer on this, but the potential duplicates are more concerned with presentation than inte...

我环顾四周,在这一个答案,但潜在的重复更关心的是presentation不是交互。

I've looked around for an answer on this, but the potential duplicates are more concerned with presentation than interaction.

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

I have a basic list box, and each item's content is a simple string.

列表框本身被拉伸以填补它的网格容器,但每个ListBoxItem中的hitarea不镜像列表框的宽度。看起来,如果hitArea(指针接触面积)为每个项目的文字内容,只有宽度。如何使这片横跨所有的方式,无论是文字大小。

The ListBox itself is stretched to fill it's grid container, but each ListBoxItem's hitarea does not mirror the ListBox width. It looks as if the hitarea (pointer contact area) for each item is only the width of the text content. How do I make this stretch all the way across, regardless of the text size.

我已经设置Horizo​​ntalContentAlignment得以舒展,但是这并没有解决我的问题。我唯一​​的猜测是,内容实际上是伸展的,但背景是不可见的,所以没有捕捉鼠标指针。

I've set HorizontalContentAlignment to Stretch, but this doesn't solve my problem. My only other guess is that the content is actually stretching, but the background is invisible and so not capturing the mouse pointer.

<ListBox 
    Grid.Row="1"
    x:Name="ProjectsListBox" 
    DisplayMemberPath="Name"
    ItemsSource="{Binding Path=Projects}" 
    SelectedItem="{Binding Path=SelectedProject}"
    HorizontalContentAlignment="Stretch"/>

该XAML是pretty的这一直截了当。如果我鼠标悬停在其中一个项目的文本,那么该项目的整个宽度变得活跃。我想我只需要知道如何创造一个互动的背景下,是看不到的。

The XAML is pretty straight forward on this. If I mouse over the text in one of the items, then the entire width of the item becomes active. I guess I just need to know how to create an interactive background that is invisible.

推荐答案

Horizo​​ntalContentAlignment =拉伸应ItemContainerStyle为此工作进行设置。

HorizontalContentAlignment="Stretch" should be set in ItemContainerStyle for this to work.

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

更新 试试这个

Update Try this

<ListBox ...>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>
阅读全文

相关推荐

最新文章