c# - 避免 UserControl 捕获鼠标滚轮滚动

标签 c# wpf .net-4.0 user-controls mouseevent

我们有一个 UserControl 显示一个枚举的所有可能值,在 ListBox 中表示为 RadioButton 以选择其中一个。当此控件位于 ScrollViewer 中,其中包含其他控件(如文本框或其他控件)并且您尝试通过鼠标滚轮滚动时,当鼠标光标悬停时,它不会滚动窗体的 ScrollViewer枚举框。

这是它在 UI 中的样子:

EnumBox in UI

为了演示,RadioButton 的背景为黄色,WrapPanel 的背景为绿色。当鼠标光标在彩色区域内(比如在 WrapPanel 内)时,通过鼠标滚轮滚动无效。

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>

我尝试在 ListBox 上设置 ScrollViewer.VerticalScrollBarVisibility="Disabled"ScrollViewer.CanContentScroll="False" WrapPanelRadioButton 及其 Border 无效。

我试图在所有四个控件上捕获 ScrollBar.Scroll="WrapPanel_Scroll" 事件,但没有一个被命中。

我尝试在 RadioButton 上设置 SelectiveScrollingGrid.SelectiveScrollingOrientation="None" 但没有效果。

有没有人知道是什么阻止了 UI 中的滚动?

要说清楚:这不是关于在 EnumBox 内滚动,而是滚动整个表单。

最佳答案

问题是 ListBox 有自己的 ScrollViewer。复制 ListBox 的模板并删除嵌入的 ScrollViewer

这是一个完整的示例,其中嵌入的 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}" 则滚轮在列表框上方工作。如果不是,则样本会出现您提到的问题。

关于c# - 避免 UserControl 捕获鼠标滚轮滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10243513/

相关文章:

c# - 从 .NET 执行 Cygwin 进程?

c# - Web API2 代理不重定向请求

c# - 即时从字符串编译 Func<T1, TResult>

c# - 设置文件夹权限

c# - AZMan:InitializeClientContextFromName 失败,返回 "Value does not fall within the expected range."

entity-framework - 处理非跟踪 self 跟踪实体的正确方法是什么?

c# - 如何在两个 MVVM 对之间进行通信?

c# - WPF DataTrigger 仅工作 1 次

c# - MahApps DateTimePicker 24 小时时间格式

c# - 打印多页xaml控件