簡體   English   中英

如何在按住Ctrl的同時禁用ScrollViewer中的滾動

[英]How to disable scrolling in ScrollViewer while Ctrl is pressed

我想在按住Ctrl鍵的同時實現縮放功能。 但是,當鼠標懸停在ScrollView上時,不會觸發MouseWheel事件。

有什么辦法嗎?

ps:SilverLight 4.0

<UserControl x:Class="SilverlightApplication11.MainPage"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         d:DesignHeight="300"
         d:DesignWidth="400">

<Grid x:Name="LayoutRoot"
      Background="White">
    <ScrollViewer Background="Gray"
                  MouseWheel="ScrollViewer_MouseWheel"
                  x:Name="scrollViewer">
        <Rectangle Width="200"
                   Height="2000"
                   MouseWheel="ScrollViewer_MouseWheel"
                   Fill="AliceBlue" />
    </ScrollViewer>
</Grid>

    private void ScrollViewer_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
        {
            zoom+=0.1;
            e.Handled = true;
        }
    }

有一種不使用樣式的簡單方法。 將ScrollViewer內容放入Border內,例如:

<ScrollViewer>
  <Border MouseWheel="ScrollViewer_MouseWheel" Background="#01b0b0b0"> <!-- almost transparent to intercept events -->
      <!-- ... your content goes here ... -->
  </Border>
</ScrollViewer>

我也有這個問題。 我通過在單擊ScrollViewer時將鍵盤焦點設置在ScrollViewer上來解決此問題。

您的代碼對我有用。 ScrollViewer永遠不會引發事件,因為ScrollViewer.OnMouseWheel已經將其標記為已處理,但是內部Rectangle實際上首先獲取了該事件。 也許HasFlag()有錯誤?

我找到了一種解決方法:

為ScrollViewer創建樣式,並在ScrollContentPresenter之前添加邊框,如下所示

<Border MouseWheel="ScrollViewer_MouseWheel"
                                        Background="Transparent"/>
<ScrollContentPresenter x:Name="ScrollContentPresenter"
                                           Cursor="{TemplateBinding Cursor}"
                                           ContentTemplate="{TemplateBinding ContentTemplate}"
                                           Margin="{TemplateBinding Padding}" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM