wpf – WP7 – 在外部ScrollViewer中滚动ListBox

wpf – WP7 – 在外部ScrollViewer中滚动ListBox,第1张

概述我的应用程序中有以下页面布局: <Grid x:Name="ContentPanel" Grid.Row="1"> <ScrollViewer x:Name="ScrollViewer1" MaxHeight="600" VerticalAlignment="Top" Horizon @H_419_1@ 我的应用程序中有以下页面布局:

<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>

我添加了一个外部ScrollVIEwer而不是使用ListBox,因为没有它,ListBox上面的东西占用了太多空间而没有留下足够的空间来查看ListBox内容.

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

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

谢谢你的帮助!

解决方法 您可以找到ListBox生成的容器来托管您的元素.拥有此容器后,您可以找到相对于scrollvIEwer的位置:

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);

希望有所帮助

总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1004858.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存