2014-09-27 1 views
0

WrapPanel을 ScrollViewer로 둘러 쌌으므로 버튼을 클릭 할 때 요소가 화면에 표시되고 싶습니다.ScrollViewer 및 WrapPanel 내부의 보이는 요소를 찾는 방법

내 코드와 같은 : ScrollViewer에 오프셋 일부 스크롤 할 때 나는 눈에 보이는 라벨 요소를 찾을 수있는 방법

<ScrollViewer> 
    <WrapPanel> 
     <Label Width="500" Height="500"/> 
     <Label Width="500" Height="500"/> 
     <Label Width="500" Height="500"/> 
     .... 
     .... 
     .... 
     <Label Width="500" Height="500"/> 
     <Label Width="500" Height="500"/> 
     <Label Width="500" Height="500"/> 
    </WrapPanel> 
</ScrollPanel> 

.

답변

0

희망이 당신이 시작됩니다 (당신은 '포함'과 'svViewportBounds.IntersectsWith'을 변경할 수 있습니다.

public partial class MainWindow : Window 
{ 
    public string VisibleItems 
    { 
     get { return (string)GetValue(VisibleItemsProperty); } 
     set { SetValue(VisibleItemsProperty, value); } 
    } 

    public static readonly DependencyProperty VisibleItemsProperty = 
     DependencyProperty.Register("VisibleItems", typeof(string), typeof(MainWindow), new PropertyMetadata("??")); 

    public List<string> Items { get; private set; } 

    public MainWindow() 
    { 
     Items = new List<string>(); 

     for (int i = 0; i < 25; ++i) 
     { 
      Items.Add("item_" + i); 
     } 

     DataContext = this; 
     InitializeComponent(); 
    } 

    void OnScrollViewerScrollChanged(object sender, ScrollChangedEventArgs e) 
    { 
     ScrollViewer sv = (ScrollViewer)sender; 
     var visibleItems = new List<int>(); 
     Rect svViewportBounds = new Rect(sv.HorizontalOffset, sv.VerticalOffset, sv.ViewportWidth, sv.ViewportHeight); 

     for(int i = 0; i < Items.Count; ++i) 
     { 
      var container = itemsHost.ItemContainerGenerator.ContainerFromIndex(i) as FrameworkElement; 
      if(container != null) 
      { 
       var offset = VisualTreeHelper.GetOffset(container); 
       var bounds = new Rect(offset.X, offset.Y, container.ActualWidth, container.ActualHeight); 

       if (svViewportBounds.IntersectsWith(bounds)) 
       { 
        visibleItems.Add(i); 
       } 
      } 
     } 

     VisibleItems = string.Join(", ", visibleItems.ToArray()); 
    } 
} 

<Window x:Class="WpfApplication59.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" 
    WindowStartupLocation="CenterScreen" 
    Width="400" 
    Height="400"> 
<DockPanel> 
    <Border BorderThickness="1" 
      BorderBrush="Black" 
      DockPanel.Dock="Top"> 
     <TextBlock Text="{Binding VisibleItems}" 
        Margin="5" /> 
    </Border> 

    <ItemsControl ItemsSource="{Binding Items}" Name="itemsHost"> 
     <ItemsControl.Template> 
      <ControlTemplate> 
       <ScrollViewer HorizontalScrollBarVisibility="Disabled" 
           ScrollChanged="OnScrollViewerScrollChanged"> 
        <WrapPanel IsItemsHost="True" /> 
       </ScrollViewer> 
      </ControlTemplate> 
     </ItemsControl.Template> 

     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Border BorderThickness="1" 
         BorderBrush="Black" 
         Width="100" 
         Height="100"> 
        <Label Content="{Binding}" /> 
       </Border> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 

     <ItemsControl.ItemContainerStyle> 
      <Style TargetType="{x:Type ContentPresenter}"> 
       <Setter Property="Margin" 
         Value="4" /> 
      </Style> 
     </ItemsControl.ItemContainerStyle> 
    </ItemsControl> 
</DockPanel>