我有一个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>