2012-04-20 116 views
3

我们有一个UserControl显示ListBox中表示为RadioButton的枚举的所有可能值以选择其中之一。当此控件与ScrollViewer以及其他控件(如文本框或任何其他控件)一起使用时,如果尝试使用鼠标滚轮进行滚动,则当鼠标光标位于EnumBox上方时,它不会滚动窗体的ScrollViewer避免UserControl捕捉鼠标滚轮滚动

这是怎么看起来像在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> 

我试图设置ScrollViewer.VerticalScrollBarVisibility="Disabled"ScrollViewer.CanContentScroll="False"ListBoxWrapPanelRadioButton及其Border有没有影响。

我试图抓住所有四个控件上的ScrollBar.Scroll="WrapPanel_Scroll"事件,但没有一个被击中。

我试图设置SelectiveScrollingGrid.SelectiveScrollingOrientation="None"RadioButton没有任何效果。

有没有人有什么阻止在用户界面中滚动的线索?

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

+0

尝试一件事情,点击控制,然后使用鼠标滚轮进行滚动,如果它能正常工作,则需要在窗体上激活时将焦点设置为控件 – Habib 2012-04-20 09:32:12

+0

@ Habib.OUS:这没有帮助。我希望很清楚,我想要在整个表单/页面/网格/窗口或EnumBox所属的UI中进行上下滚动。我不想在EnumBox中滚动。 – gumo 2012-04-20 12:54:31

回答

3

问题是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}"然后滚轮工作在列表框中时。如果不是,那么样本就会显示你提到的问题。

+0

啊,谢谢菲尔,我应该知道:)它就像一个魅力。 – gumo 2012-04-25 11:01:24

0

我认为你的问题是你的背景透明设置在你的ListBoxBorder等等。它阻止它接收焦点以及鼠标事件。给你的ListBox一个颜色,但翻转它的RGBA的一部分。试试这个例如:#000A0A09(它的黑色与A = 0%,这使得它看起来透明)

+0

非常感谢Denis,但在这种情况下改变颜色并没有帮助。 – gumo 2012-04-23 07:57:25