如何仅在Treeview子元素上启用scrollviewer

时间:2018-12-27 11:58:40

标签: c# wpf xaml

在wpf TreeView中,我只想在TreeViewItem的子级上启用Scrollviewer。我在第一级使用HierarchicalDataTemplate(它将被阻止),在儿童级使用另一个Hierarchical。 我想做的是锁定第一级并使子元素的itempanel可滚动。下面是我的代码

<UserControl x:Class="WoltersKluwer.Kleos.Outlook.View.Filed"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WoltersKluwer.Kleos.Outlook.View"
         xmlns:userControl="clr-namespace:WoltersKluwer.Kleos.Outlook.View.UserControls"
         xmlns:converter="clr-namespace:WoltersKluwer.Kleos.Outlook.View.Converters"
         xmlns:ui="clr-namespace:WoltersKluwer.Kleos.Outlook.Infrastractures.WPF;assembly=WoltersKluwer.Kleos.Outlook.Infrastractures.WPF"
         xmlns:vm="clr-namespace:WoltersKluwer.Kleos.Outlook.ViewModel;assembly=WoltersKluwer.Kleos.Outlook.ViewModel"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         xmlns:behaviour="clr-namespace:WoltersKluwer.Kleos.Outlook.View.TreeViewBehavior"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <ResourceDictionary>

        <HierarchicalDataTemplate x:Key="FolderTemplate"
                                  DataType="{x:Type vm:FolderViewModel}"
                                  ItemsSource="{Binding Children}">
            <Border Padding="{StaticResource BorderPaddingSubItem}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Grid Grid.Row="0" Visibility="{Binding IsVisible, Converter={StaticResource BoolToVisibilityConverter}}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <userControl:VectorImage 
                                   Grid.Column="0"
                                   Grid.Row="0"
                                   ImagePath="folder.xaml"
                                   Width="12" Height="12"
                                   DefaultColor="{StaticResource BackgroundIsEnabledBrush}"
                                   VerticalAlignment="Center"
                                   Margin="{StaticResource ElementSmallRightMargin}" />

                        <TextBlock Text="{Binding Name}"
                                    FontSize="{StaticResource FontSize12}"
                                    Grid.Row="0"
                                    TextTrimming="CharacterEllipsis" 
                                    Grid.Column="1" />
                        <StackPanel Orientation="Horizontal"
                                    HorizontalAlignment="Right"                                      
                                    Grid.Row="0"
                                    Grid.Column="2">

                            <Button Content="{Binding Resources[Resources/Kleos.Outlook.AddIn/Change_Button/], Mode=OneTime, FallbackValue=Change}"
                                    Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                                      AncestorType={x:Type local:Filed}},
                                                      Path=DataContext.OpenFoldersScenarionCommand}"
                                    CommandParameter="{Binding}"
                                    Style="{StaticResource ButtonFlatStyle}"
                                    VerticalAlignment="Center"
                                    VerticalContentAlignment="Center"
                                    Margin="10,3,0,0" />
                        </StackPanel>

                    </Grid>                        

                    <Grid Grid.Row="1" Grid.Column="0" Margin="0,5,0,5" Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                                            AncestorType={x:Type local:Filed}},
                                                            Path=DataContext.ImportAsDetached,
                                                            Converter={StaticResource BoolToVisibilityConverter}}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <CheckBox IsChecked="{Binding IsSelected}" Grid.Column="0" />
                        <userControl:VectorImage Grid.Column="1" Width="12" ImagePath="{Binding AttachmentDto.Name, Converter={StaticResource fileExtToImagePathConverter}}" Margin="{StaticResource ElementSmallRightMargin}" />
                        <TextBlock Text="{Binding AttachmentDto.Name}" Grid.Column="2" TextTrimming="CharacterEllipsis" />
                    </Grid>

                </Grid>
            </Border>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate x:Key="CaseTemplate"
                                  DataType="{x:Type vm:CaseViewModel}"
                                  ItemsSource="{Binding Children}"
                                  ItemTemplate="{StaticResource FolderTemplate}">
            <Border Padding="{StaticResource BorderPaddingFirstItem}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <userControl:VectorImage Grid.Column="0"
                           ImagePath="folder_document.xaml" Width="20" Height="20"
                           Margin="{StaticResource ElementMediumRightMargin}"
                           VerticalAlignment="Top" />
                    <Grid Grid.Column="1">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0"
                                   Text="{Binding Name}"
                                   Margin="{StaticResource ElementSmallTopNegativeMargin}"
                                   HorizontalAlignment="Left"
                                   FontWeight="Light"
                                   Foreground="{StaticResource ForegroundBrush}"
                                   FontSize="{StaticResource FontSize16}"
                                   TextTrimming="CharacterEllipsis" />
                        <TextBlock Grid.Row="1"
                                   Text="{Binding Description}"
                                   FontSize="{StaticResource FontSize12}"
                                   HorizontalAlignment="Left"
                                   Foreground="{StaticResource StyleToolbarItemsBrush}"
                                   TextTrimming="CharacterEllipsis" />
                    </Grid>
                    <Button Content="{Binding Resources[Resources/Kleos.Outlook.AddIn/Change_Button/], Mode=OneTime, FallbackValue=Change}"
                                Grid.Column="2"
                                Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                                  AncestorType={x:Type local:Filed}},
                                                  Path=DataContext.OpenCasesScenarionCommand}"
                                CommandParameter="{Binding}"
                                Style="{StaticResource ButtonFlatStyle}"                                    
                                Margin="0,0,-17,0" />
                </Grid>
            </Border>
        </HierarchicalDataTemplate>

        <Style x:Key="TreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
            <Setter Property="Focusable" Value="True" />
            <Setter Property="IsExpanded" Value="True" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="Width" Value="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}" />
            <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
            <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
            <Setter Property="TextElement.FontFamily" Value="{StaticResource DefaultFontFamily}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <Grid Name="PART_grid">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <Border Name="Bd">
                                <ContentPresenter x:Name="PART_Header"
                                                  ContentSource="Header"
                                                  Width="{Binding Path=ActualWidth, ElementName=Bd}"
                                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                            </Border>
                                <ItemsPresenter x:Name="ItemsHost"
                                            Grid.Row="1"
                                            Grid.Column="1" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </ResourceDictionary>
</UserControl.Resources>

<Grid>
    <Border Padding="{StaticResource BorderPaddingGrid}">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"  />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="1"
                       Text="File here"
                       Style="{StaticResource TitleCasesTextBlockStyle}" />

             <TreeView
                     Grid.Row="2"
                      VirtualizingStackPanel.IsVirtualizing="True"
                      VirtualizingStackPanel.VirtualizationMode="Recycling"
                      ScrollViewer.VerticalScrollBarVisibility="Auto"
                      ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                      BorderThickness="0"
                      Margin="0,10,0,35"
                      ItemsSource="{Binding Boxes}"
                      ItemTemplate="{StaticResource CaseTemplate}"
                      ItemContainerStyle="{StaticResource TreeViewItemStyle}"
                      Background="Transparent" />

        </Grid>
    </Border>
</Grid>

下面的说明图:

enter image description here

我该如何解决? 非常感谢。

0 个答案:

没有答案