" CanContentScroll"使用嵌套的ScrollViewers,属性不能很好地工作

时间:2013-08-27 07:35:18

标签: c# wpf scrollviewer

我在父CanContentScroll中将True属性设置为Scrollviewer。此ScrollViewer适用于整个Window。现在,这个ScrollViewer已将ItemsControl作为其子级。 ItemsControl中的每个项目都被模板化为Expander,此Expander的内容包含嵌入在子ItemsControl中的另一个ScrollViewer。如果我删除了子ScrollViewer,则Window内容是可滚动的,但是,如果我将其重新插入,则无法滚动。

我需要孩子ScrollViewer,因为我限制了Expander内容的高度。滚动时,事件不会被触发到Visual Tree中的父ScrollViewer。如何以及通过使用哪个事件我可以将它冒泡到Visual Tree中的父ScrollViewer

这是我的代码:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:behaviors="clr-namespace:AttributeSelector" xmlns:sys="clr-namespace:System;assembly=mscorlib"
    MinHeight="350" MinWidth="525" MaxWidth="1200" MaxHeight="900" WindowStartupLocation="CenterScreen" SizeToContent="WidthAndHeight">
    <sys:Boolean x:Key="BooleanTrue">True</sys:Boolean>
    <sys:Boolean x:Key="BooleanFalse">False</sys:Boolean>
    <behaviors:TextToHighlightedTextBlockConverter x:Key="TextToHighlightedTextBlockConverter" />
    <behaviors:GroupNameToICollectionViewConverter x:Key="GroupNameToICollectionViewConverter" />
    <LinearGradientBrush x:Key="LinearGradientBrush" StartPoint="0.5,0" EndPoint="0.5,1">
        <GradientStop Color="White" Offset="0"/>
        <GradientStop Color="#E3E3E3" Offset="1"/>
        <!--<GradientStop Color="#C0C0C0" Offset="1"/>-->
    </LinearGradientBrush>
    <Style x:Key="MainBorderStyle" TargetType="{x:Type Border}">
        <Setter Property="Background" Value="{StaticResource ResourceKey=LinearGradientBrush}" />
        <Setter Property="BorderBrush" Value="LightGray" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="CornerRadius" Value="5" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
    </Style>
</Window.Resources>
<DockPanel LastChildFill="True" Margin="10">
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="10">
        <!-- 3-4 TextBlocks/TextBoxes defined here -->
    </StackPanel>
    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" CanContentScroll="True">
        <ItemsControl ItemsSource="{Binding UniqueGroups}" ScrollViewer.CanContentScroll="True"
                  ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel IsItemsHost="True" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border Margin="5" Style="{StaticResource ResourceKey=MainBorderStyle}">
                        <Expander x:Name="GroupExpander" Header="{Binding}" IsExpanded="True" Margin="5">
                            <Expander.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding}" Background="Transparent" Margin="10,0,0,0"
                                               FontWeight="DemiBold" FontSize="16" FontStretch="Expanded"
                                               HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                               Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Expander}}, Path=ActualWidth}"/>
                                </DataTemplate>
                            </Expander.HeaderTemplate>
                            <Expander.Content>
                                <ScrollViewer Margin="20,5,10,5" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" CanContentScroll="True">
                                    <ItemsControl ItemsSource={Binding SubItems} ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
                                        <ItemsControl.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <WrapPanel MaxHeight="300" Orientation="Vertical" UseLayoutRounding="True" IsItemsHost="True" />
                                            </ItemsPanelTemplate>
                                        </ItemsControl.ItemsPanel>
                                        <ItemsControl.GroupStyle>
                                            <GroupStyle>
                                                <GroupStyle.Panel>
                                                    <ItemsPanelTemplate>
                                                        <VirtualizingStackPanel Orientation="Horizontal" IsItemsHost="True" />
                                                    </ItemsPanelTemplate>
                                                </GroupStyle.Panel>
                                            </GroupStyle>
                                        </ItemsControl.GroupStyle>
                                    </ItemsControl>
                                </ScrollViewer>
                            </Expander.Content>
                        </Expander>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</DockPanel>

0 个答案:

没有答案