Pra*_*ian 4 silverlight wpf windows-phone-7
我的应用程序中有以下页面布局:
<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>
Run Code Online (Sandbox Code Playgroud)
我添加了一个外部ScrollViewer
而不是使用的ListBox
,因为没有它,上面的东西ListBox
占用了太多的空间,没有留下足够的空间来查看ListBox
内容.
现在,问题是如果我ListBox
在ScrollIntoView
方法的末尾添加一个项目不起作用.所以我需要使用ScrollViewer
的ScrollToVerticalOffset
方法.
我将新项目添加到ObservableCollection
绑定到ListBox
用户单击应用程序栏上的按钮时的项目.如何计算要传递给的值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);
Run Code Online (Sandbox Code Playgroud)
希望有所帮助
归档时间: |
|
查看次数: |
4210 次 |
最近记录: |