How to disable scrolling in ScrollViewer while Ctrl is pressed
I'd like to implement zoom function while Ctrl key is pressed. But the MouseWheel event is not trigger while the mouse is over the ScrollView.
Is there any way to do it?
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"
Mous开发者_高级运维eWheel="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;
}
}
There's an easier approach without use of style. Put ScrollViewer content inside Border like:
<ScrollViewer>
<Border MouseWheel="ScrollViewer_MouseWheel" Background="#01b0b0b0"> <!-- almost transparent to intercept events -->
<!-- ... your content goes here ... -->
</Border>
</ScrollViewer>
I also had this problem. I fix it by setting the keyboard focus on the ScrollViewer when you click on the ScrollViewer .
Your code works for me. The ScrollViewer never raises the event because ScrollViewer.OnMouseWheel already marked it as handled, but the inner Rectangle actually gets the event first. Maybe HasFlag() has a bug?
I found a workaround:
Create a style for the ScrollViewer, and add a Border before ScrollContentPresenter like following
<Border MouseWheel="ScrollViewer_MouseWheel"
Background="Transparent"/>
<ScrollContentPresenter x:Name="ScrollContentPresenter"
Cursor="{TemplateBinding Cursor}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Margin="{TemplateBinding Padding}" />
精彩评论