2011-01-06 5 views
4

에서 나는 내 응용 프로그램에 다음 페이지 레이아웃을 가지고 :WP7 - 스크롤리스트 박스 외부에서 ScrollViewer

<Grid x:Name="ContentPanel" 
     Grid.Row="1"> 

    <ScrollViewer x:Name="ScrollViewer1" 
       MaxHeight="600" 
       VerticalAlignment="Top" 
       HorizontalAlignment="Stretch"> 

    <StackPanel x:Name="StackPanel1" > 
     <TextBlock x:Name="TextBlock1" /> 

     <toolkit:ListPicker x:Name="ListPicker1" /> 

     <TextBlock x:Name="TextBlock2" /> 

     <TextBox x:Name="TextBlock3" /> 

     <TextBlock x:Name="TextBlock4" /> 

     <StackPanel x:Name="StackPanel2" > 

     <TextBlock x:Name="TextBlock5" /> 

     <Image x:Name="Image1"/> 

     </StackPanel> 

     <ListBox x:Name="ListBox1"> 
     <!--Customize the ListBox template to remove the built-in ScrollViewer--> 
     <ListBox.Template> 
      <ControlTemplate> 
      <ItemsPresenter /> 
      </ControlTemplate> 
     </ListBox.Template> 

     <ListBox.ItemTemplate> 
      <DataTemplate> 

      <!-- .... --> 

      </DataTemplate> 
     </ListBox.ItemTemplate> 

     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
      <Setter Property="HorizontalContentAlignment" 
        Value="Stretch" /> 
      </Style> 
     </ListBox.ItemContainerStyle> 
     </ListBox> 

    </StackPanel> 

    </ScrollViewer> 

</Grid> 

내가 때문에 ListBox 위의 물건을 복용 한없이 대신 사용하여 ListBox 년대의 외부 ScrollViewer 추가 너무 많은 공간과 ListBox 콘텐츠를 볼 수있는 충분한 공간을 남기지 않았습니다.

이제는 ListBox 끝 부분에 항목을 추가하면 ScrollIntoView 메서드가 작동하지 않는 문제가 있습니다. 그래서 ScrollViewerScrollToVerticalOffset 메소드를 사용해야합니다.

ObservableCollection에 새 항목을 추가합니다.이 항목은 사용자가 응용 프로그램 막대에서 단추를 클릭 할 때 ListBox에 바인딩됩니다. 에 전달할 값을 어떻게 계산할 수 있습니까?

도움 주셔서 감사합니다.

답변

6

요소를 호스팅하기 위해 ListBox에서 생성 한 컨테이너를 찾을 수 있습니다. 이 컨테이너가 있으면, 당신은 ScrollViewer에 자사의 위치를 ​​찾을 수 있습니다

+0

작품을 아름답게하는 데 도움이

var newItem = // the item you just added to your listbox // find the ListBox container listBox.UpdateLayout() var element = listBox.ItemContainerGenerator.ContainerFromItem(newItem) as FrameworkElement; // find its position in the scroll viewer var transform = element.TransformToVisual(ScrollViewer); var elementLocation = transform.Transform(new Point(0, 0)); double newVerticalOffset = elementLocation.Y + ScrollViewer.VerticalOffset; // scroll into view ScrollViewer.ScrollToVerticalOffset(newVerticalOffset); 

희망! 정말 고맙습니다! 이 코드 앞에 myListBox.UpdateLayout();을 써야했고, 그렇지 않으면'ContainerFromItem()'이'null'을 계속 리턴했다. 또한,'ContainerFromItem()'의 결과는'ListBoxItem'으로 형변환 될 필요가 있습니다. – Praetorian

+0

쿨 - 다행스럽게 생각합니다. – ColinE