我希望从C#动态更改Silverlight ListBox的滚动位置,我需要知道如何从C#访问ListBox控件的ScrollViewer元素?
谢谢你们,杰夫
我认为这应该很容易,但我很难接受它.
如何在C#中获取对ListBox的scrollviewer的引用?我已经尝试了几乎所有我能想到的东西.ListBox位于WPF自定义控件中,因此我们使用Template.FindName来获取对所有控件的引用.我的ListBox看起来像这样:
<ListBox x:Name="PART_SoundList"
ScrollViewer.CanContentScroll="False"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Hidden" Focusable="False" FocusVisualStyle="{x:Null}"
HorizontalAlignment="Center" VerticalAlignment="Bottom" BorderThickness="0"
ItemContainerStyleSelector="{StaticResource ListBoxItemAlternatingStyleSelector}"
ItemsSource="{Binding}" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" Height="850" Focusable="False" Panel.ZIndex="999" >
<WrapPanel.RenderTransform>
<TransformGroup>
<ScaleTransform CenterX="0" CenterY="0" ScaleX=".75" ScaleY=".57" />
</TransformGroup>
</WrapPanel.RenderTransform>
</WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Template>
<ControlTemplate>
<ScrollViewer x:Name="Scroller" VerticalAlignment="Bottom" Focusable="False" Style="{StaticResource HorizontalScroller}" >
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Focusable="False" Panel.ZIndex="999" />
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
Template.FindName("Scroller",this)作为ScrollViewer导致null.
有任何想法吗?