silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了wpf – WP7 – 在外部ScrollViewer中滚动ListBox大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我的应用程序中有以下页面布局: <Grid x:Name="ContentPanel" Grid.Row="1"> <ScrollViewer x:Name="ScrollViewer1" MaxHeight="600" VerticalAlignment="Top" Horizon
我的应用程序中有以下页面布局: @H_618_19@ @H_618_19@
<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>
@H_618_19@我添加一个外部ScrollViewer而不是使用ListBox,因为没有它,ListBox上面的东西占用了太多空间而没有留下足够的空间来查看ListBox内容.

@H_618_19@现在,问题是如果我在ListBox的末尾添加一个项目,ScrollIntoView方法不起作用.所以我需要使用ScrollViewer的ScrollToVerticalOffset方法.

@H_618_19@当用户单击应用程序栏上的按钮时,我将新项添加到绑定到ListBox的ObservableCollection.如何计算要传递给ScrollViewer.ScrollToVerticalOffset的值?

@H_618_19@谢谢你的帮助!

解决方法

您可以找到ListBox生成的容器来托管您的元素.拥有此容器后,您可以找到相对于scrollviewer的位置: @H_618_19@ @H_618_19@
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);
@H_618_19@希望有所帮助

大佬总结

以上是大佬教程为你收集整理的wpf – WP7 – 在外部ScrollViewer中滚动ListBox全部内容,希望文章能够帮你解决wpf – WP7 – 在外部ScrollViewer中滚动ListBox所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。