C# 避免用户控件捕捉鼠标滚轮滚动
我们有一个C# 避免用户控件捕捉鼠标滚轮滚动,c#,wpf,.net-4.0,user-controls,mouseevent,C#,Wpf,.net 4.0,User Controls,Mouseevent,我们有一个UserControl在ListBox中显示一个枚举的所有可能值,表示为RadioButton,以选择其中一个。当此控件位于具有其他控件(如文本框或其他控件)的ScrollViewer中,并且您尝试通过鼠标滚轮滚动时,当鼠标光标位于EnumBox上时,它不会滚动窗体的ScrollViewer 这是它在UI中的外观: 演示时,单选按钮的背景为黄色,包装盒的背景为绿色。当鼠标光标位于彩色区域内时(例如在包装盒),通过鼠标滚轮滚动没有效果 EnumBox的模板如下所示: <Use
UserControl
在ListBox
中显示一个枚举的所有可能值,表示为RadioButton
,以选择其中一个。当此控件位于具有其他控件(如文本框或其他控件)的ScrollViewer
中,并且您尝试通过鼠标滚轮滚动时,当鼠标光标位于EnumBox上时,它不会滚动窗体的ScrollViewer
这是它在UI中的外观:
演示时,单选按钮的背景为黄色,包装盒的背景为绿色。当鼠标光标位于彩色区域内时(例如在包装盒
),通过鼠标滚轮滚动没有效果
EnumBox的模板如下所示:
<UserControl.Template>
<ControlTemplate TargetType="{x:Type clientsWpf:EnumBox}">
<StackPanel>
<GroupBox Header="{Binding Header, RelativeSource={RelativeSource AncestorType={x:Type clientsWpf:EnumBox}}}" IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource AncestorType={x:Type clientsWpf:EnumBox}}}">
<Border x:Name="InvalidBorder" BorderBrush="Red" BorderThickness="0" >
<ListBox x:Name="PART_ListBox" HorizontalAlignment="Left" KeyboardNavigation.DirectionalNavigation="Cycle" Background="Transparent" BorderThickness="0" SelectedValuePath="." SelectedValue="{Binding Path=SelectedValue, RelativeSource={RelativeSource AncestorType={x:Type clientsWpf:EnumBox}}}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" Background="Green"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Resources>
<Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Background="Transparent" Background="Yellow">
<RadioButton Margin="3" Focusable="False" Content="{TemplateBinding ContentControl.Content,Converter={StaticResource enumValueDescriptionConverter}}"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}},Path=IsSelected}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
</ListBox>
</Border>
</GroupBox>
</StackPanel>
</ControlTemplate>
</UserControl.Template>
我试图在列表框
上设置ScrollViewer.VerticalScrollBarVisibility=“Disabled”
和ScrollViewer.CanContentScroll=“False”
,WrapPanel
,RadioButton
及其边框,但没有效果
我试图捕获所有四个控件上的ScrollBar.Scroll=“WrapPanel\u Scroll”
事件,但没有一个被击中
我试图在单选按钮
上设置SelectiveScrollingGrid.SelectiveScrollingOrientation=“None”
,但没有效果
有人知道是什么阻止了用户界面中的滚动吗
明确地说:这不是在枚举框内滚动,而是滚动整个表单。我认为您的问题是您在列表框
,边框
上设置了透明背景。。它阻止它接收焦点和鼠标事件。给你的列表框
一种颜色,但在上面翻转一部分RGBA。。试试这个例子:#000A0A09(它的黑色带有A=0%,这使它看起来很透明)问题在于列表框有自己的滚动查看器。复制列表框的模板
并删除嵌入的滚动查看器
下面是一个完整的示例,其中嵌入的ScrollViewer
被注释掉了:
<Window x:Class="WpfApplication1.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">
<Window.Resources>
<SolidColorBrush x:Key="ListBorder" Color="#828790"/>
<Style x:Key="ListBoxStyleNoScrollViewer" TargetType="{x:Type ListBox}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true">
<!--<ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">-->
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<!--</ScrollViewer>-->
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<ScrollViewer>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition Height="100"/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<ListBox Grid.Row="1" Style="{StaticResource ListBoxStyleNoScrollViewer}" >
<ListBox.Items>
<ListBoxItem>One</ListBoxItem>
<ListBoxItem>Two</ListBoxItem>
<ListBoxItem>Three</ListBoxItem>
<ListBoxItem>Four</ListBoxItem>
</ListBox.Items>
</ListBox>
</Grid>
</ScrollViewer>
</Window>
一个
两个
三
四
如果列表框具有Style=“{StaticResource ListBoxStyleNoScrollViewer}”
,则滚轮在列表框上方时工作。如果没有,则示例显示您提到的问题。尝试一件事,单击控件,然后使用鼠标滚轮滚动,如果它工作,则需要在控件上激活时将焦点设置为控件form@Habib.OUS:这没用。我希望很清楚,我想上下滚动整个表单/页面/网格/窗口,或者枚举框在UI中的任何部分。我不想在EnumBox中滚动。谢谢Denis,但是在这种情况下更改颜色没有帮助。啊,谢谢Phil,我应该知道:)它像一个符咒一样工作。