2013-01-16 15 views
0

我有一個單選按鈕作爲其項目的列表框。列表框是可滾動的,並且可以添加儘可能多的項目。但是,我必須保持第一項凍結,因爲它應該始終可見。請有任何想法嗎?如何凍結wpf中listbox的第一項?

回答

0

它有點棘手。您可以編輯Listbox的模板並在scrollviewer上方添加一個ListBoxItem。類似於

<Style x:Key="ListBoxTopItemFixedStyle" TargetType="{x:Type ListBox}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ListBox}"> 
        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true"> 
         <Grid> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto"/> 
           <RowDefinition Height="*"/> 
          </Grid.RowDefinitions> 

          <ListBoxItem Content="{Binding Path=FreezeItem.PropertyName, 
                RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
             Grid.Row="0"/> 
          <ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}" Grid.Row="1"> 
           <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
          </ScrollViewer> 
         </Grid> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
         </Trigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="IsGrouping" Value="true"/> 
           <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
         </MultiTrigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

我也使用了FreezeItem,它是來自控件上下文中的集合的項目。您可以使用相同的概念並根據需要對其進行修改。如果你想避免FreezeItem,你也可以使用轉換器來獲得第一個收集項。

+0

今天我會定義嘗試這種方法,並會回覆。 – Virus