我有一个
WPF列表框,它使用大量行进行虚拟化.当我滚动实体时,列表框的大小会发生变化.我试过了:
<Setter Property="MinWidth" Value="{Binding Path=ExtentWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollViewer}}}" />
可悲的是,我没有足够的声誉来发布我的问题图片,但基本上当我滚动浏览虚拟列表框时,框的宽度会随着遇到更长的项目而改变.我想我可以尝试测量代码隐藏中最长的字符串并将宽度设置为该值,但我希望有一个更清晰的解决方案.
这是我当前的控制模板(没有骰子):
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="ListItemContainer"
MinWidth="{Binding Path=ExtentWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollViewer}}}"
BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter></ContentPresenter>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="ListItemContainer" Value="{DynamicResource AxisValueSelectedBackground}"/>
<Setter Property="Foreground" Value="{DynamicResource AxisValueSelectedForeground}"/>
</Trigger>
<Trigger Property="IsSelected" Value="false">
<Setter Property="Background" TargetName="ListItemContainer" Value="{DynamicResource AxisValueBackground}"/>
<Setter Property="Foreground" Value="{Binding IsEnabled, Converter={StaticResource AxisValueForegroundConverter}}" />
<Setter Property="FontStyle" Value="{Binding IsEnabled, Converter={StaticResource AxisValueFontStyleConverter}}" />
</Trigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsAxisSelected}" Value="True"/>
<Condition Binding="{Binding IsAxisValueSelected}" Value="False"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" TargetName="ListItemContainer" Value="White" />
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
最佳答案 尝试相同的相对绑定技巧,但在DataTemplate的最外层项(可能是Grid)中.这样项目的大小都相同.