2014-05-02 24 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

相关问题