弹力列表框项目灾区到ListBox的整个宽度?列表框样式是通过一个主题设置隐含性 [英] Stretch ListBox Items hit area to full width of the ListBox? ListBox style is set implicity through a theme

查看:165
本文介绍了弹力列表框项目灾区到ListBox的整个宽度?列表框样式是通过一个主题设置隐含性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,在这一个答案,但潜在的重复更关心的是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>

这篇关于弹力列表框项目灾区到ListBox的整个宽度?列表框样式是通过一个主题设置隐含性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆