如何自动滚动scrollviewer到specefic位置

时间:2013-02-02 05:43:32

标签: wpf scrollviewer

我有一个自定义用户控件ExpanderListUC,它包含一个扩展器列表,每个扩展器都有一个包含标签列表的stackpanel。

我添加了一个scrollviewer来向上和向下滚动扩展器内容。

在某些情况下,我会通过代码隐藏在扩展器4中自动选择标签4。

如何自动将滚动查看器的位置设置为所选标签?

 <ScrollViewer x:Name="sv" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden" HorizontalAlignment="Left" Width="205">
        <StackPanel Name="spContentPresenter" Background="Transparent" HorizontalAlignment="Right" >
            <StackPanel.ContextMenu>
                <ContextMenu x:Name ="ExpanderContextMenu" StaysOpen="true">
                    <MenuItem x:Name="collapseAllMenuItem" Header="Collapse All" Foreground="Black" />
                    <MenuItem x:Name="expandAllMenuItem" Header="Expand All" Foreground="Black"  />
                </ContextMenu>
            </StackPanel.ContextMenu>
            <Expander FlowDirection="LeftToRight" IsEnabled="True" IsExpanded="True"  >
                <!--Background="#FF1F5897"-->
                <Expander.Header>
                    <!--<Run Text="Electrical Information" Foreground="Black"/>-->
                    <TextBlock HorizontalAlignment="Right" Foreground="Black"><Run Text="Expander 1"/></TextBlock>
                </Expander.Header>
                <Grid Margin="0" >
                    <StackPanel Width="200" HorizontalAlignment="Right">
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 1" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 2" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 3" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 4" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 5" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 6" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 7" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 8" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 9" Height="26" />
                        <!--Style="{DynamicResource ClickableLabel}"-->
                    </StackPanel>
                </Grid>
            </Expander>
            <Expander FlowDirection="LeftToRight" IsEnabled="True" IsExpanded="True"  >
                <!--Background="#FF1F5897"-->
                <Expander.Header>
                    <!--<Run Text="Electrical Information" Foreground="Black"/>-->
                    <TextBlock HorizontalAlignment="Right" Foreground="Black"><Run Text="Expander 2"/></TextBlock>
                </Expander.Header>
                <Grid Margin="0" >
                    <StackPanel Width="200" HorizontalAlignment="Right">
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 1" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 2" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 3" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 4" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 5" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 6" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 7" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 8" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 9" Height="26" />
                        <!--Style="{DynamicResource ClickableLabel}"-->
                    </StackPanel>
                </Grid>
            </Expander>
            <Expander FlowDirection="LeftToRight" IsEnabled="True" IsExpanded="True"  >
                <!--Background="#FF1F5897"-->
                <Expander.Header>
                    <!--<Run Text="Electrical Information" Foreground="Black"/>-->
                    <TextBlock HorizontalAlignment="Right" Foreground="Black"><Run Text="Expander 3"/></TextBlock>
                </Expander.Header>
                <Grid Margin="0" >
                    <StackPanel Width="200" HorizontalAlignment="Right">
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 1" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 2" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 3" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 4" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 5" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 6" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 7" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 8" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 9" Height="26" />
                        <!--Style="{DynamicResource ClickableLabel}"-->
                    </StackPanel>
                </Grid>
            </Expander>
            <Expander FlowDirection="LeftToRight" IsEnabled="True" IsExpanded="True"  >
                <!--Background="#FF1F5897"-->
                <Expander.Header>
                    <!--<Run Text="Electrical Information" Foreground="Black"/>-->
                    <TextBlock HorizontalAlignment="Right" Foreground="Black"><Run Text="Expander 4"/></TextBlock>
                </Expander.Header>
                <Grid Margin="0" >
                    <StackPanel Width="200" HorizontalAlignment="Right">
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 1" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 2" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 3" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 4" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 5" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 6" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 7" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 8" Height="26" />
                        <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 9" Height="26" />
                        <!--Style="{DynamicResource ClickableLabel}"-->
                    </StackPanel>
                </Grid>
            </Expander>                
        </StackPanel>
    </ScrollViewer>

1 个答案:

答案 0 :(得分:1)

我有类似的问题。最好的方法是让你的Expander成为一个独立的控件并使用ItemsControl,如下所示:

<UserControl x:Class="WpfApplication9.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d"
         d:DesignHeight="423" d:DesignWidth="368">
<Grid Background="White">
    <Expander FlowDirection="LeftToRight" IsEnabled="True" IsExpanded="True"  >
        <!--Background="#FF1F5897"-->
        <Expander.Header>
            <!--<Run Text="Electrical Information" Foreground="Black"/>-->
            <TextBlock HorizontalAlignment="Right" Foreground="Black"><Run Text="Expander 1"/></TextBlock>
        </Expander.Header>
        <Grid Margin="0" >
            <StackPanel Width="200" HorizontalAlignment="Right">
                <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 1" Height="26" />
                <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 2" Height="26" />
                <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 3" Height="26" />
                <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 4" Height="26" />
                <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 5" Height="26" />
                <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 6" Height="26" />
                <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 7" Height="26" />
                <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 8" Height="26" />
                <Label Margin="0,0,10,0"  HorizontalAlignment="Right" Content="Label 9" Height="26" />
                <!--Style="{DynamicResource ClickableLabel}"-->
            </StackPanel>
        </Grid>
    </Expander>
</Grid>

    <Grid x:Name="Grid">
    <ScrollViewer x:Name="ScrollViewer" >
        <Grid>
            <ItemsControl x:Name="ItemsControl" ItemsSource="{Binding /*some list*/}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate DataType="/*some type to bind to*/">
                        <my:UserControl1/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </ScrollViewer>
</Grid>


    void ScrollToIndex(int index) {
        UIElement uiElement = (UIElement) ItemsControl.ItemContainerGenerator.ContainerFromIndex(index);
        if (uiElement != null) {
            double y = uiElement.TranslatePoint(new System.Windows.Point(0, 0), Grid).Y;
            ScrollViewer.ScrollToVerticalOffset(y);
        }
    }