我無法在Silverlight 4的列表框控件中禁用鼠標滾輪滾動的默認行爲,並將自定義滾動動畫應用於mousewheel事件上的列表框。當我將動畫添加到列表框的鼠標滾輪事件中時,第一個列表框項目會根據鼠標滾輪移動滾動,然後我的自定義動畫滾動列表框項目會被觸發。請回答我的查詢。如何在Silverlight 4的列表框中禁用鼠標滾輪滾動?
2
A
回答
1
我找不到任何簡單的方法來關閉它,但您可以更改列表框的模板以使用不同的容器(它在內部使用ScrollViewer)。
這樣最簡單的版本是from another answer:
<ListBox.Template>
<ControlTemplate>
<ItemsPresenter />
</ControlTemplate>
</ListBox.Template>
你真的需要爲一個列表框模板看起來像這樣的Expression Blend進行修改,一個完整的模板:
<UserControl.Resources>
<ControlTemplate x:Key="ValidationToolTipTemplate">
<Grid x:Name="Root" Margin="5,0" Opacity="0" RenderTransformOrigin="0,0">
<Grid.RenderTransform>
<TranslateTransform x:Name="xform" X="-25"/>
</Grid.RenderTransform>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="OpenStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0"/>
<VisualTransition GeneratedDuration="0:0:0.2" To="Open">
<Storyboard>
<DoubleAnimation Duration="0:0:0.2" To="0" Storyboard.TargetProperty="X" Storyboard.TargetName="xform">
<DoubleAnimation.EasingFunction>
<BackEase Amplitude=".3" EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
<DoubleAnimation Duration="0:0:0.2" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Root"/>
</Storyboard>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="Closed">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Root"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Open">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="X" Storyboard.TargetName="xform"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Root"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border Background="#052A2E31" CornerRadius="5" Margin="4,4,-4,-4"/>
<Border Background="#152A2E31" CornerRadius="4" Margin="3,3,-3,-3"/>
<Border Background="#252A2E31" CornerRadius="3" Margin="2,2,-2,-2"/>
<Border Background="#352A2E31" CornerRadius="2" Margin="1,1,-1,-1"/>
<Border Background="#FFDC000C" CornerRadius="2"/>
<Border CornerRadius="2">
<TextBlock Foreground="White" MaxWidth="250" Margin="8,4,8,4" TextWrapping="Wrap" Text="{Binding (Validation.Errors)[0].ErrorContent}" UseLayoutRounding="false"/>
</Border>
</Grid>
</ControlTemplate>
<Style x:Key="ListBoxStyle1" TargetType="ListBox">
<Setter Property="Padding" Value="1"/>
<Setter Property="Background" Value="#FFFFFFFF"/>
<Setter Property="Foreground" Value="#FF000000"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="TabNavigation" Value="Once"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA3AEB9" Offset="0"/>
<GradientStop Color="#FF8399A9" Offset="0.375"/>
<GradientStop Color="#FF718597" Offset="0.375"/>
<GradientStop Color="#FF617584" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ValidationStates">
<VisualState x:Name="Valid"/>
<VisualState x:Name="InvalidUnfocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="InvalidFocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="validationTooltip">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<System:Boolean>True</System:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2">
<ScrollViewer x:Name="ScrollViewer" BorderBrush="Transparent" BorderThickness="0" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" TabNavigation="{TemplateBinding TabNavigation}">
<ItemsPresenter/>
</ScrollViewer>
</Border>
<Border x:Name="ValidationErrorElement" BorderBrush="#FFDB000C" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2" Visibility="Collapsed">
<ToolTipService.ToolTip>
<ToolTip x:Name="validationTooltip" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Template="{StaticResource ValidationToolTipTemplate}">
<ToolTip.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="validationTooltip">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<System:Boolean>true</System:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ToolTip.Triggers>
</ToolTip>
</ToolTipService.ToolTip>
<Grid Background="Transparent" HorizontalAlignment="Right" Height="10" Margin="0,-4,-4,0" VerticalAlignment="Top" Width="10">
<Path Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="#FFDC000C" Margin="-1,3,0,0"/>
<Path Data="M 0,0 L2,0 L 8,6 L8,8" Fill="#ffffff" Margin="-1,3,0,0"/>
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
0
試試這個:上layoutUpdate
事件從scrollview
獲得ScrollContentPresenter
:
private ScrollContentPresenter Scp;
void ScrollView_LayoutUpdated(object sender, EventArgs e)
{
if (Scp == null)
{
try
{
FrameworkElement myFE = (FrameworkElement)VisualTreeHelper.GetChild(ScrollView, 0);
Scp = (ScrollContentPresenter)myFE.FindName("ScrollContentPresenter");
Scp.MouseWheel += new MouseWheelEventHandler(Scp_MouseWheel);
}
catch (Exception)
{
}
}
然後在SCP鼠標滾輪事件:
void Scp_MouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
}
相關問題
- 1. 在組合框/列表框中啓用鼠標滾輪滾動
- 2. 如何禁用JScrollPane標題中鼠標滾輪的滾動?
- 3. 更改行數WPF中的鼠標滾輪滾動列表框
- 4. 如何禁用GtkOptionMenu/GtkComboBox中的滾動鼠標滾輪
- 5. Silverlight 4鼠標滾輪按下
- 6. iScroll 4水平鼠標滾輪滾動
- 7. 滾動在列表框鼠標滾輪與修改ItemsPanel
- 8. 禁用滾動條和鼠標滾輪但不滾動到
- 9. Eclipse的SWT:如何禁用鼠標滾輪scrolledcomposite滾動
- 10. GWT禁用鼠標滾輪滾動了滾動面板
- 11. 在銫中禁用鼠標滾輪
- 12. Popup - 滾動鼠標滾輪
- 13. 滾動面板鼠標滾輪滾動
- 14. 的FlowLayout面板禁用鼠標中間的滾輪滾動
- 15. 如何讓鼠標滾輪正常使用Silverlight 4 ScrollViewer
- 16. 鼠標滾輪不在tmux中滾動
- 17. 禁用滾動條並啓用滾動鼠標滾輪和僅滾動
- 18. 禁用鼠標滾輪/或任何滾動jquery
- 19. 禁用QscrollArea上的鼠標滾輪滾動
- 20. 使用鼠標滾輪的Flex列表滾動速度
- 21. WPF在ListView上禁用鼠標滾輪滾動
- 22. 如何讓圖像(輪子)在jQuery中滾動或滾動鼠標滾輪?
- 23. 組合框的鼠標滾輪滾動也滾動
- 24. 禁用DevExpress的鼠標滾輪滾動XtraScrollableControl
- 25. Silverlight ListBox不滾動鼠標滾輪在Mac上運行
- 26. 如何使用鼠標滾輪在WPF中水平滾動?
- 27. 使用鼠標滾輪添加滾動
- 28. 鼠標滾輪的滾動操作
- 29. 如何用鼠標滾輪移動JSlider
- 30. 使用鼠標滾輪和鼠標移動滾動
我不能刪除的ScrollViewer列表框控件中因爲我的動畫的ScrollViewer垂直的動畫滾動的偏移。所以請任何其他建議。 – 2010-09-18 16:59:28
問題*是* ScrollViewer。您可以*設置其他容器類型的偏移量。您不需要爲ScrollViewer設置動畫,所以擺脫它並使用另一個容器(或多個容器)。 – 2010-09-19 20:02:14