2015-10-28 21 views
0

我有一個複合XAML控件。它看起來像這樣:當一個複合XAML控件被選中時,如何確保整個父控件是可見的?

<UI:CompoundControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Dynamics="clr-namespace:MyNamespace.UI" Height="Auto" > 
    <Stackpanel Orietnation="Vertical"> 
     <TextArea x:Name="lblTop" DockPanel.Dock="Left">Label</TextArea > 
     <TextArea x:Name="lblBottom" DockPanel.Dock="Left">Label</TextArea > 
    </Stackpanel > 
</UI:CompoundControl> 

當應用程序運行,在這CompoundControl s的UIElementCollection,這顯示他們在一個滾動列表。

當我按選項卡鍵時,應用程序將焦點依次移動到下一個TextArea,滾動視圖以進行操作。但是,它只滾動顯示集中控制,所以我遇到了一個問題,其中按標籤並不顯示CompoundControl的全部,只有上半部分。

是否有一些屬性,我可以設置,使自動滾動顯示整個控制,而不是簡單的上半部分?

+0

將ScrollViewer的「CanContentScroll」屬性設置爲true – netaholic

回答

0

想通過迴應頂部控件的「GotFocus」事件來使其工作。在這種情況下,scrollViewer是將複合控件添加到的父級ScrollViewer。

private const int SCROLL_PADDING = 10; 
private void lblTop_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e) 
     { 
      if (scrollViewer == null) return; 
      FrameworkElement element = e.OriginalSource as FrameworkElement; 
      var transform = element.TransformToVisual(scrollViewer); 
      var positionInScrollViewer = transform.Transform(new Point(0, 0)); 

      //Scrolls the viewer down far enough to see this control + the control directly below it. 
      if (positionInScrollViewer.Y < 0 || 
       positionInScrollViewer.Y + SCROLL_PADDING > scrollViewer.ViewportHeight) 
      { 
       scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + 
        positionInScrollViewer.Y - SCROLL_PADDING); 
      } 


     } 

這將滾動的ScrollViewer過去的化合物控制的整體,但只有當化合物控制的頂部部分是關閉的,或幾乎關閉,屏幕。

相關問題