2010-10-28 95 views
3

當前使用滾動查看器,單擊其中包含的滾動條以平移周圍的內容將不會聚焦查看器(或任何有關此事的內容)。 我希望通過拖動滾動進行滾動,或者單擊滾動條將會使父級(滾動查看器)聚焦。如何在滾動查看器中獲取滾動條以單擊滾動查看器時將其對焦?

我通過在scrollviewer上附加ScrollChanged處理程序並調用sender.focus()來實現這一點。 Unforutnally ScrollChanged被調用初始化scrollviewer等等加載

我也嘗試附加事件制定者仍然沒有運氣。

理想情況下,我喜歡某種風格或某種讓我可以將其應用於整個應用程序的所有滾動查看器的東西。

編輯:只是爲了進一步澄清,如果我使用這個xaml並單擊滾動條它不會將背景變成藍色(gotfocus事件)。當我點擊滾動查看器或按鈕時,它會。

<Window x:Class="GotFocusProblem.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid Margin="20" x:Name="grid"> 
      <Grid.Background> 
       <SolidColorBrush x:Name="backgrnd" Color="Transparent"/> 
      </Grid.Background> 
      <ScrollViewer Margin="10" Height="100" Background="#FFEEEEEE"> 
       <Button Content="Button" Name="button1" Height="300" Width="75" /> 
      </ScrollViewer> 
      <Grid.Triggers> 
       <EventTrigger RoutedEvent="Grid.GotFocus"> 
        <BeginStoryboard> 
         <Storyboard> 
          <ColorAnimation 
          Storyboard.TargetName="backgrnd" 
          Storyboard.TargetProperty="Color" 
          To="Cyan" 
          BeginTime="0:0:0" 
          Duration="0:0:0" /> 
         </Storyboard> 
        </BeginStoryboard> 
       </EventTrigger> 
      </Grid.Triggers> 
     </Grid> 
    </Grid> 
</Window> 

回答

4

我不是100%確定你在這裏尋找,但將此代碼添加到您的例子將會使ScrollViewer中接收焦點點擊滾動條時,一拖等一些代碼背後需要這個解決方案。

<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}"> 
    <EventSetter Event="PreviewMouseDown" Handler="scrollBar_PreviewMouseDown"/>    
</Style> 

而且在後面

void scrollBar_PreviewMouseDown(object sender, MouseButtonEventArgs e) 
{ 
    c_scrollViewer.Focus(); 
} 

更新代碼

你可能知道如何把它添加到資源字典,以便可以多ScrollViewers訪問它,否則,這裏是你如何可以做到。

將資源字典添加到您的項目。我打電話給我的ScrollBarStyles.xaml。
在類的後面添加一個名爲ScrollBarStyles.xaml.cs的代碼。
添加X:在XAML文件類的屬性,像

x:Class="YourNameSpace.ScrollBarStyles" 

ScrollBarStyles.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        x:Class="FocusScrollViewer.ScrollBarStyles"> 
    <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}"> 
     <EventSetter Event="PreviewMouseDown" Handler="scrollBar_PreviewMouseDown"/> 
    </Style> 
</ResourceDictionary> 

ScrollBarStyles.xaml.cs

public partial class ScrollBarStyles 
{ 
    public T GetVisualParent<T>(object childObject) where T : Visual 
    { 
     DependencyObject child = childObject as DependencyObject; 
     // iteratively traverse the visual tree 
     while ((child != null) && !(child is T)) 
     { 
      child = VisualTreeHelper.GetParent(child); 
     } 
     return child as T; 
    } 

    void scrollBar_PreviewMouseDown(object sender, MouseButtonEventArgs e) 
    { 
     ScrollBar scrollBar = sender as ScrollBar; 
     ScrollViewer scrollViewer = GetVisualParent<ScrollViewer>(scrollBar); 
     scrollViewer.Focus(); 
    } 
} 

你的窗口

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="ScrollBarStyles.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 
+0

這是一個非常好的答案。謝謝。雖然之前我已經爲特定的滾動條做了一個代碼,然後像我一樣不知道如何在樣式中執行此操作。事實上,我沒有意識到資源字典可能有代碼隱藏。很有價值的信息。謝謝! – Seravy 2010-10-29 02:36:08

+0

它就像一個魅力! – GuruC 2011-01-20 07:20:01