Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在WPF中正确设置虚拟化列表框的宽度_Wpf_Listbox_Virtualization - Fatal编程技术网

在WPF中正确设置虚拟化列表框的宽度

在WPF中正确设置虚拟化列表框的宽度,wpf,listbox,virtualization,Wpf,Listbox,Virtualization,我有一个WPF列表框,它是用大量行虚拟化的。当我滚动实体时,列表框的大小会发生变化。我试过: <Setter Property="MinWidth" Value="{Binding Path=ExtentWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollViewer}}}" /> 遗憾的是,我没有足够的声誉来发布我的问题图片,但基本上,当我滚动虚拟化列表框时,框的宽度会随

我有一个WPF列表框,它是用大量行虚拟化的。当我滚动实体时,列表框的大小会发生变化。我试过:

<Setter Property="MinWidth" Value="{Binding Path=ExtentWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollViewer}}}" />

遗憾的是,我没有足够的声誉来发布我的问题图片,但基本上,当我滚动虚拟化列表框时,框的宽度会随着遇到的项目变长而改变。我想我可以尝试测量codebehind中最长的字符串,并将宽度设置为该值,但我希望有更干净的解决方案

这是我当前的控件模板(无骰子):



尝试相同的相对绑定技巧,但在DataTemplate的最外层项(可能是网格)内。这样,所有项目的大小都相同

        <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>