2013-02-02 5 views
0

사용자 정의 사용자 정의 컨트롤 인 ExpanderListUC가 있습니다. 각 확장기에는 목록 레이블이 들어있는 스택 패널이 있습니다.scrollviewer를 특정 위치로 자동 스크롤하는 방법

저는 확장기 내용을 위아래로 스크롤하기 위해 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

나는 비슷한 문제가있었습니다. 그것을 할 수있는 가장 좋은 방법은 다음과 같이 당신의 확장기를 분리 된 제어를하고 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); 
     } 
    } 
+0

. 코드를 시도했는데 제공된 메소드가 작동하지 않았습니다. – Cracker

관련 문제