2012-04-20 108 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