2014-05-02 25 views
0

我正在使用Windows Phone 8應用程序,並且我想實現一個按鈕,該按鈕可以跳轉到ListHeader的頂部LongListSelectorListHeader包含一些用於過濾內容的單選按鈕) 。有沒有辦法做到這一點?有沒有辦法跳轉到LongListSelector的ListHeader?

+1

您是否嘗試過使用'LongListSelector.ScrollTo()'滾動到第一項?不知道這是否會將頭部撞到視圖中,但您可以嘗試一下。 – Pantelis

+0

@Pantelis我有。雖然這會滾動到項目列表的頂部,但它不會滾動到足以顯示ListHeader。 – Kamaros

回答

0

在您的按鈕處理程序中,使用此方法從LongListSelector的模板中獲取ViewPortControl

public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject 
    { 
     // Confirm parent and childName are valid. 
     if (parent == null) 
     { 
      return null; 
     } 

     T foundChild = null; 

     int childrenCount = VisualTreeHelper.GetChildrenCount(parent); 
     for (int i = 0; i < childrenCount; i++) 
     { 
      DependencyObject child = VisualTreeHelper.GetChild(parent, i); 
      // If the child is not of the request child type child 
      var childType = child as T; 
      if (childType == null) 
      { 
       // recursively drill down the tree 
       foundChild = FindChild<T>(child, childName); 

       // If the child is found, break so we do not overwrite the found child. 
       if (foundChild != null) 
       { 
        break; 
       } 
      } 
      else if (!string.IsNullOrEmpty(childName)) 
      { 
       var frameworkElement = child as FrameworkElement; 
       // If the child's name is set for search 
       if (frameworkElement != null && frameworkElement.Name == childName) 
       { 
        // if the child's name is of the request name 
        foundChild = (T)child; 
        break; 
       } 

       // Need this in case the element we want is nested 
       // in another element of the same type 
       foundChild = FindChild<T>(child, childName); 
      } 
      else 
      { 
       // child element found. 
       foundChild = (T)child; 
       break; 
      } 
     } 

     return foundChild; 
    } 

然後使用THIS滾動到ViewPortControl的頂部。

用法:

private void Button_OnClick(object sender, RoutedEventArgs e) 
{ 
    var viewPort = FindChild<ViewportControl>(yourLongListSelector, "ViewportControl"); 
    viewPort.SetViewportOrigin(new Point(0,0)); 
} 

如果你想知道在哪裏的ViewPortControl是從哪裏來的,這裏的LongListSelector的風格。

<Style x:Key="LongListSelectorStyle1" TargetType="phone:LongListSelector"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="phone:LongListSelector"> 
        <Grid Background="{TemplateBinding Background}" d:DesignWidth="480" d:DesignHeight="800"> 
         <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"/> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="NotScrolling"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Grid Margin="{TemplateBinding Padding}"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="*"/> 
           <ColumnDefinition Width="auto"/> 
          </Grid.ColumnDefinitions> 
          <ViewportControl x:Name="ViewportControl" HorizontalContentAlignment="Stretch" VerticalAlignment="Top"/> 
          <ScrollBar x:Name="VerticalScrollBar" Grid.Column="1" Margin="4,0,4,0" Opacity="0" Orientation="Vertical"/> 
         </Grid> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
+0

工程就像一個魅力!不過,獎勵另外3個小時不能。 – Kamaros

+0

賞金賞賜 - 你值得擁有! – Kamaros

相關問題