其他分享
首页 > 其他分享> > 避免UserControl捕获鼠标滚轮滚动

避免UserControl捕获鼠标滚轮滚动

作者:互联网

我们有一个UserControl,它在列表框中显示一个以RadioButton表示的枚举的所有可能值,以选择其中之一.当此控件位于带有其他控件(如文本框或任何其他控件)的ScrollViewer内部时,如果您尝试通过鼠标滚轮滚动,则当鼠标光标位于EnumBox上时,它不会滚动窗体的ScrollViewer.

这是用户界面中的样子:

为了演示,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,WrapPanel,RadioButton及其Border上将ScrollViewer.VerticalScrollBarVisibility =“ Disabled”和ScrollViewer.CanContentScroll =“ False”设置为无效.

我试图在所有四个控件上捕获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}”,则滚轮在列表框上方时将起作用.如果不是,则样本显示出您提到的问题.

标签:mouseevent,user-controls,wpf,c,net-4-0
来源: https://codeday.me/bug/20191201/2080831.html