如何使ScrollViewer在鼠标悬停在*任何*内容上时滚动

9
我要强调“任意”是因为在我的ScollViewer中,CanContentScroll没有完全发挥作用。 让我解释一下场景: 我有一个ScrollViewer,里面有三个标签,每个标签后面都跟着一个ListBox。我将这些内容放在ScrollViewer中的原因是我不想让每个ListBox都有一个ScrollBar,我只想要一个“全局”ScrollBar。问题是当鼠标悬停在ListBox上时,ScrollViewer不会滚动。我尝试在ScrollViewerListBoxListBoxItem样式中将CanContentScroll属性设置为true,但没有成功。是否有其他控件类型可以使用? 这是我的代码示例:
<UserControl x:Class="Telbit.TeStudio.View.Controls.TestStepsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:Telbit.TeStudio.View.Controls">

<UserControl.Resources>
    <DataTemplate DataType="{x:Type my:TestStepsStepViewModel}">
        <my:TestStepsStepView HorizontalAlignment="Stretch"/>
    </DataTemplate>

    <Style x:Key="StepItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="Border" SnapsToDevicePixels="true" Background="Transparent" BorderThickness="0" Padding="1">
                        <ContentPresenter/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="#40a0f5ff"/>
                        </Trigger>
                        <Trigger Property="IsKeyboardFocusWithin" Value="True">
                            <Setter Property="IsSelected" Value="True" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<UserControl.Background>
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FFF2F2F2"/>
        <GradientStop Color="Gainsboro" Offset="1"/>
    </LinearGradientBrush>
</UserControl.Background>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <HeaderedContentControl Grid.Row="0" >
        <HeaderedContentControl.Header>
            <Grid Background="#e8f2f8">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition MinWidth="200" Width="*" />
                    <ColumnDefinition Width="75"/>
                    <ColumnDefinition Width="60"/>
                    <ColumnDefinition Width="60"/>
                    <ColumnDefinition Width="60"/>
                    <ColumnDefinition Width="60"/>
                    <ColumnDefinition Width="120"/>
                    <ColumnDefinition Width="130"/>
                </Grid.ColumnDefinitions>

                <Label Grid.Column="0" Content="#" BorderBrush="#70add4" BorderThickness="2 2 0 2"/>
                <Label Grid.Column="1"
                       Content="Folder\Name" 
                       BorderBrush="#70add4" BorderThickness="0 2 0 2"/>
                <Label Grid.Column="2" Content="Type" BorderBrush="#70add4" BorderThickness="0 2 0 2" Margin="-20 0 0 0"/>
                <Label Grid.Column="3" Content="Auto Start" BorderBrush="#70add4" BorderThickness="0 2 0 2" Margin="-20 0 0 0"/>
                <Label Grid.Column="4" Content="Run After" BorderBrush="#70add4" BorderThickness="0 2 0 2" Margin="-20 0 0 0"/>
                <Label Grid.Column="5" Content="Stop After" BorderBrush="#70add4" BorderThickness="0 2 0 2" Margin="-20 0 0 0"/>
                <Label Grid.Column="6" Content="Delay (s)" BorderBrush="#70add4" BorderThickness="0 2 0 2" Margin="-20 0 0 0"/>
                <Label Grid.Column="7" Content="Timestamp" BorderBrush="#70add4" BorderThickness="0 2 0 2" Margin="-20 0 0 0"/>
                <Label Grid.Column="8" Content="Edited by" BorderBrush="#70add4" BorderThickness="0 2 2 2" Margin="-20 0 0 0"/>
            </Grid>
        </HeaderedContentControl.Header>
    </HeaderedContentControl>
    <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1" VerticalAlignment="Top" CanContentScroll="True">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="3*"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="3*"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="3*"/>
            </Grid.RowDefinitions>

            <Label Name="lblSetup" Grid.Row="0" 
                   VerticalContentAlignment="Center"
                   BorderBrush="DarkGray" BorderThickness="0 0 0 1"
                   TextBlock.FontSize="10pt" TextBlock.FontWeight="Bold" TextBlock.Foreground="#949494"
                   Content="Setup" AllowDrop="True"/>
            <ListBox Name="itmCtrlSetupSteps" Grid.Row="1"
                     BorderThickness="0" Background="Transparent"
                     ItemsSource="{Binding SetupSteps}" SelectionMode="Single"
                     HorizontalContentAlignment="Stretch" 
                     ItemContainerStyle="{StaticResource StepItemStyle}"
                     SelectionChanged="manageStep_SelectionChanged"
                     ScrollViewer.CanContentScroll="True"
                     />

            <Label Name="lblTest" Grid.Row="2" 
                   VerticalContentAlignment="Center"
                   BorderBrush="DarkGray" BorderThickness="0 0 0 1"
                   TextBlock.FontSize="10pt" TextBlock.FontWeight="Bold" TextBlock.Foreground="#949494"
                   Content="Test" AllowDrop="True"/>
            <ListBox Name="itmCtrlTestSteps" Grid.Row="3"
                     BorderThickness="0" Background="Transparent"
                     ItemsSource="{Binding TestSteps}" SelectionMode="Single"
                     HorizontalContentAlignment="Stretch"
                     ItemContainerStyle="{StaticResource StepItemStyle}"
                     SelectionChanged="manageStep_SelectionChanged"
                     />

            <Label Name="lblTearDown" Grid.Row="4" 
                   VerticalContentAlignment="Center"
                   BorderBrush="DarkGray" BorderThickness="0 0 0 1"
                   TextBlock.FontSize="10pt" TextBlock.FontWeight="Bold" TextBlock.Foreground="#949494"
                   Content="Tear Down" AllowDrop="True"/>
            <ListBox Name="itmCtrlTearDownSteps" Grid.Row="5"
                     BorderThickness="0" Background="Transparent"
                     ItemsSource="{Binding TearDownSteps}" SelectionMode="Single"
                     HorizontalContentAlignment="Stretch"
                     ItemContainerStyle="{StaticResource StepItemStyle}"
                     SelectionChanged="manageStep_SelectionChanged"
                     />
        </Grid>
    </ScrollViewer>
</Grid>
</UserControl>
1个回答

18
问题在于,即使子列表框没有可见的滚动条,但它们根据其模板确实拥有 ScrollViewer 。幸运的是,这个模板很容易修改。为每个子列表框执行此操作,或者更好的方法是将其放入公共样式中:
<ListBox.Template>
    <ControlTemplate TargetType="ListBox">
        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderBrush}">
            <ItemsPresenter/>
        </Border>
    </ControlTemplate>
</ListBox.Template>

默认情况下几乎相同,唯一的区别是 ScrollViewer 包裹在 ItemsPresenter 的周围。


问题是我不想为每个ListBox(我有3个)使用一个ScrollViewer。我想要一个“全局”的ScrollViewer,给人一种唯一的ListBox的感觉。从我理解的来看,你的代码会为每个ListBox提供一个ScrollViewer,对吗? - jpsstavares
不,我的代码中没有ScrollViewer。这就是重点。将其应用于子元素以消除它们的滚动。 - repka
1
很抱歉怀疑你,我应该先尝试解决方案。 它正在运作,非常感谢! - jpsstavares

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接