2010-11-01 99 views
4

我有一個WPF列表框,它被大量的行虛擬化。當我滾動實體時,列表框的大小會發生變化。我已經試過:在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> 

alt text alt text

回答

2

嘗試同樣的相對結合伎倆,但您DataTemplate中的最外面的項目(可能是一個網格)內。這樣的項目都是相同的大小。