我有ListBox
一个PopupControl
。问题是在滚动时,Popup 会调整大小以适应实际最宽的元素。
如何避免这种调整大小并自动调整到整个列表中最宽的元素?
我试图把它放在一个Grid
但没有成功。
好的,这是解决方案:添加此属性
<ListBox VirtualizingStackPanel.IsVirtualizing="False"
调整大小停止,因为现在面板包含所有元素,并且根据最宽的元素调整宽度。使用虚拟化面板,它只是显示的项目的一部分,列表框将宽度调整为实际可见的最宽元素。
缺点是我们不再使用虚拟化面板(默认开启)
如果要继续进行虚拟化,可以将 设置Popup.Width
为常量。
当然,要选择正确的常数,您必须计算(或至少猜测)每个常数的宽度ListBoxItem
,并选择最大值。...通常根据您的内容,粗略猜测并不难。
我遇到了与上述完全相同的问题 - 我的 ListBox 不会虚拟化,因为它会忙于在可调整大小的 PopUp 控件中进行布局。我找到的解决方案是MaxWidth
限制MaxHeight
包含ListBox
. 我的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>
大多数 WPFUIElement
控件都有一个Width
可以设置为“ Auto
”的属性,因此它们将占用与其最宽元素一样多的空间。