2010-08-18 35 views
1

Silverlight ListBox控件在單擊顯示列表項時自動移動滾動框(如果列表框顯示5個項目,請單擊最後一個項目,並將所有項目向下移動一個)。我的QA團隊發現了一個錯誤,告訴我這對我們的具體情況會造成混淆。我如何覆蓋這種行爲?如何覆蓋Silverlight ListBox的默認行爲?

<ListBox x:Name="accountsListBox" Margin="8,65,8,8" SelectionChanged="accountsListBox_SelectionChanged" VirtualizingStackPanel.VirtualizationMode="Recycling"> 
        <ListBox.BorderBrush> 
         <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
          <GradientStop Color="Black" Offset="0"/> 
          <GradientStop Color="Silver" Offset="1"/> 
         </LinearGradientBrush> 
        </ListBox.BorderBrush> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <TextBlock Height="19" Text="{Binding}"/> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 

    public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
      Loaded += new RoutedEventHandler(MainPage_Loaded); 
     } 

     void MainPage_Loaded(object sender, RoutedEventArgs e) 
     { 
      List<string> names = new List<string>(); 
      for (int i = 0; i < 100; i++) 
      { 
       names.Add("Name " + i); 
      } 

      this.accountsListBox.ItemsSource = names; 

     } 

     private void accountsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
// this was an attempt but causes other unwanted behavior 
      //int selectedIndex = this.accountsListBox.SelectedIndex; 
      //this.accountsListBox.UpdateLayout(); 
      //this.accountsListBox.ScrollIntoView(this.accountsListBox.Items[this.accountsListBox.SelectedIndex - 4]); 
      //this.accountsListBox.SelectedIndex = selectedIndex; 
     } 
    } 

回答

2

沒有必要重寫行爲。這不是一個錯誤,但滾動部分顯示列表項到視圖中的功能。

自動滾動行爲是由列表框不足以滿足5個項目(即使它可能看起來)。使列表框高几個像素,問題就會消失。

希望這會有所幫助。

+0

這是exactally權的高度年底追加!感謝看到我沒有。 – Aligned 2010-08-18 21:42:47

0

如果要防止ListBox自動滾動以顯示整個選定項目,請爲其ScrollViewer提供自定義樣式(請參見下文),並在模板中爲垂直滾動條指定1000的頂部和底部邊距。 ,給列表框一個1000的頂部和底部邊距,並將其樣式設置爲使用修改後的滾動查看器樣式。然後,剪下列表框(如下所示),以便它不會掩蓋它上面的任何內容。最後,只要您將項目添加到您的列表框,首先添加具有1000的高度空ListBoxItem中,然後在項目另一個空一個ListBoxItem與1000

<Style x:Key="ScrollViewerStyleForWhiteBg" TargetType="ScrollViewer"> 
     <Setter Property="VerticalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="HorizontalScrollBarVisibility" Value="Disabled"/> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="Padding" Value="0"/> 
     <Setter Property="BorderThickness" Value="0"/> 
     <Setter Property="BorderBrush" Value="Transparent"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ScrollViewer"> 
        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="ScrollStates"> 
           <VisualStateGroup.Transitions> 
            <VisualTransition GeneratedDuration="00:00:00.5"/> 
           </VisualStateGroup.Transitions> 
           <VisualState x:Name="Scrolling"> 
            <Storyboard> 
             <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="VerticalScrollBar"/> 
             <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="HorizontalScrollBar"/> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="NotScrolling"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Grid Margin="{TemplateBinding Padding}"> 
          <ScrollContentPresenter x:Name="ScrollContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}"/> 
          <ScrollBar x:Name="VerticalScrollBar" Margin="0,1000,0,1000" HorizontalAlignment="Right" Height="Auto" IsHitTestVisible="False" IsTabStop="False" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Opacity="0" Orientation="Vertical" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{TemplateBinding VerticalOffset}" ViewportSize="{TemplateBinding ViewportHeight}" VerticalAlignment="Stretch" Width="5" Background="Red" /> 
          <ScrollBar x:Name="HorizontalScrollBar" HorizontalAlignment="Stretch" Height="5" IsHitTestVisible="False" IsTabStop="False" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Opacity="0" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{TemplateBinding HorizontalOffset}" ViewportSize="{TemplateBinding ViewportWidth}" VerticalAlignment="Bottom" Width="Auto" /> 
         </Grid> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
<Style x:Key="ListBoxStyleForReader" TargetType="ListBox"> 
     <Setter Property="Padding" Value="0"/> 
     <Setter Property="Foreground" Value="Black"/> 
     <Setter Property="BorderThickness" Value="0"/> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderBrush" Value="Transparent"/> 
     <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBox"> 
        <ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}" 
            Style="{StaticResource ScrollViewerStyleForWhiteBg}"> 
         <ItemsPresenter /> 
        </ScrollViewer> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
<ListBox x:Name="_myList" Margin="0,-1000,0,-1000" Style="{StaticResource ListBoxStyleForReader}"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <StackPanel Orientation="Vertical"/> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
      <ListBox.Clip> 
       <RectangleGeometry Rect="0,1000,480,10000" /> 
      </ListBox.Clip> 
     </ListBox> 
_myList.Insert(0, new ListBoxItem() { Height = 1000 }); 
_myList.Add(new ListBoxItem() { Height = 1000 });