2010-09-30 52 views
3

這是我的組合框。它似乎沒有虛擬化,但我無法弄清楚原因。你知道任何會導致這種情況的東西嗎?WPF:什麼會導致ComboBox無法虛擬化?

<ComboBox Grid.Row="0" Grid.Column="2" 
    SelectedValuePath="PrimaryKey" 
    SelectedValue="{Binding CustomerKey}" 
    ItemsSource="{Binding CustomerCanidates}"> 
    <ComboBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <VirtualizingStackPanel/> 
     </ItemsPanelTemplate> 
    </ComboBox.ItemsPanel> 
</ComboBox> 

回答

3

檢查應用於控制的樣式。例如,Microsoft的「亮藍色」風格被定義爲這樣的:

<ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}"> 
    <Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}"/> 
</ControlTemplate> 


<Style TargetType="{x:Type ComboBox}"> 
    <Setter Property="SnapsToDevicePixels" Value="true"/> 
    <Setter Property="Template" Value="{DynamicResource ComboBoxTemplate}" /> 
</Style> 

<ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}"> 
    <Grid> 
     <ToggleButton Grid.Column="2" Template="{DynamicResource ComboBoxToggleButton}" x:Name="ToggleButton" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/> 
     <ContentPresenter HorizontalAlignment="Left" Margin="3,3,23,3" x:Name="ContentSite" VerticalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" IsHitTestVisible="False"/> 

     <TextBox Visibility="Hidden" Template="{DynamicResource ComboBoxTextBox}" HorizontalAlignment="Left" Margin="3,3,23,3" x:Name="PART_EditableTextBox" Style="{x:Null}" VerticalAlignment="Center" Focusable="True" Background="Transparent" IsReadOnly="{TemplateBinding IsReadOnly}"/> 

     <Popup IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" PopupAnimation="Slide"> 
      <Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True"> 
       <Border x:Name="DropDownBorder" Background="{DynamicResource ShadeBrush}" BorderBrush="{DynamicResource SolidBorderBrush}" BorderThickness="1"/> 
       <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True"> 

        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/> 

       </ScrollViewer> 
      </Grid> 
     </Popup> 
    </Grid> 
    <ControlTemplate.Triggers> 
     <Trigger Property="HasItems" Value="false"> 
      <Setter Property="MinHeight" Value="95" TargetName="DropDownBorder"/> 
     </Trigger> 
     <Trigger Property="IsEnabled" Value="false"> 
      <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/> 
     </Trigger> 
     <Trigger Property="IsGrouping" Value="true"> 
      <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
     </Trigger> 
     <Trigger Property="AllowsTransparency" SourceName="Popup" Value="true"> 
      <Setter Property="Margin" Value="0,0,0,0" TargetName="DropDownBorder"/> 
      <Setter Property="CornerRadius" TargetName="DropDownBorder" Value="3,3,3,3"/> 
     </Trigger> 
     <Trigger Property="IsEditable" Value="true"> 
      <Setter Property="IsTabStop" Value="false"/> 
      <Setter Property="Visibility" Value="Visible" TargetName="PART_EditableTextBox"/> 
      <Setter Property="Visibility" Value="Hidden" TargetName="ContentSite"/> 
     </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 

僅僅從一個StackPanel將其更改爲一個VirtualizingStackPanel固定我的問題。

1

嘗試過設置此屬性:

<ComboBox VirtualizingStackPanel.IsVirtualizing="True" 
+0

唉,沒有改變它。 – 2010-09-30 23:22:40

+0

嗯,這很奇怪。這在TreeViews和ListViews/Boxes中一直適用於我。從來沒有嘗試過ComboBoxes,但我不明白爲什麼它不會工作。 – Carlo 2010-09-30 23:23:44

+0

原來這是樣式表中的東西。我會盡快更新我的問題。 – 2010-09-30 23:26:35