WPF ListBox固定宽度

时间:2009-07-17 07:41:03

标签: wpf listbox width fixed

我在ListBox中有一个PopupControl。问题是在滚动时,Popup会调整大小 以适应实际最宽的元素。

如何避免调整大小并自动调整到整个列表中最宽的元素?

我试图把它放在Grid但没有成功。

4 个答案:

答案 0 :(得分:6)

确定这是解决方案:添加此属性

<ListBox VirtualizingStackPanel.IsVirtualizing="False"

调整大小停止,因为现在Panel包含所有元素并调整宽度 尊重最广泛的人。 使用虚拟化面板,它只是显示项目的一部分,而ListBox会将widht调整为实际可见的最宽元素。

缺点是,我们不再使用虚拟化面板(默认情况下)

答案 1 :(得分:1)

如果要继续虚拟化,可以将Popup.Width设置为常量。

当然,要选择正确的常数,您必须计算(或至少猜测)每个ListBoxItem的宽度,然后选择最大值。 ...通常根据你的内容进行粗略猜测并不难。

答案 2 :(得分:0)

我遇到了与上述完全相同的问题-我的ListBox无法虚拟化,因为它会在可调整大小的PopUp控件中忙于布局。我发现的解决方案是限制包含MaxWidth的网格的MaxHeightListBox。我的PopUp控件仍然可以使用Grip调整大小-它可以占用的空间不是无限的-我认为一旦知道这可以解决问题,就退出一个易于实现的解决方案:-)

我知道ListBox正在虚拟化,因为大约18,000个元素的查询将在1-2秒而不是30-60秒内得到回答,并且滚动很快而不是冻结。

<Popup x:Name="PART_Popup"
       AllowsTransparency="true"  
       PlacementTarget="{Binding ElementName=PART_ContentHost}"                                   
       Placement="Bottom"                                                        
       IsOpen="{Binding IsPopupOpened, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"                                     
       PopupAnimation="None"
       Focusable="False"
       StaysOpen="True"
    >
    <Border BorderBrush="{TemplateBinding PopupBorderBrush}"
            BorderThickness="{TemplateBinding PopupBorderThickness}"
            Background="{DynamicResource {x:Static reskeys:ResourceKeys.ControlPopupBackgroundBrushKey}}"  
            >
        <!-- Do NOT REMOVE MaxHeight and MaxWidth
             These ensure that containing ListBox is virtualizing -->
        <Grid x:Name="PART_ResizeableGrid" Background="Transparent"
              MaxHeight="600"
              MaxWidth="600"
              >
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Border
                x:Name="DropDownBorder"
                Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"  
                Width="{Binding ActualWidth, ElementName=PART_ContentHost}"                                             
                Height="{Binding ActualHeight, ElementName=PART_ContentHost}"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                Grid.RowSpan="2"
                BorderThickness="0"
                />

            <ListBox
                x:Name="PART_ItemList" Grid.Row="0"
                HorizontalAlignment="Stretch" VerticalAlignment="Top"
                ItemsSource="{Binding Suggestions, RelativeSource={RelativeSource TemplatedParent}}"
                BorderThickness="0"
                ItemTemplate="{TemplateBinding ItemTemplate}"
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"                                                             
                Template="{DynamicResource {x:Static reskeys:ResourceKeys.PopListBoxControlTemplate}}"
                ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                ScrollViewer.VerticalScrollBarVisibility="Auto" 
                ScrollViewer.CanContentScroll="True"

                DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
                SelectedValuePath="{TemplateBinding ValuePath}"

                KeyboardNavigation.AcceptsReturn="True"
                KeyboardNavigation.DirectionalNavigation="Cycle"

                BorderBrush="{TemplateBinding BorderBrush}"
                VirtualizingPanel.IsVirtualizing="True"
                VirtualizingPanel.VirtualizationMode="Recycling"
                ScrollViewer.IsDeferredScrollingEnabled="True"
                />

            <!-- RezizeGrip Thumb to support resizing the suggestion lib -->
            <Thumb x:Name="PART_ResizeGripThumb"
                   Grid.Row="0"
                   Style="{DynamicResource {x:Static reskeys:ResourceKeys.ResizeGripStyleKey}}"
                   HorizontalAlignment="Right"
                   VerticalAlignment="Bottom"
                   Margin="0"
                   Background="Transparent"
                   Width="16"
                   Height="16" />
        </Grid>
    </Border>
</Popup>

答案 3 :(得分:-1)

大多数WPF UIElement控件都有一个Width属性,可以将其设置为“Auto”,这样它们就会占用与其最宽元素一样多的空间。