无法在ItemsPanelTemplate内的WrapPanel中拖动自定义ScrollViewer

时间:2011-11-23 09:01:07

标签: wpf wpf-controls

在usercontrol中的列表框上实现自定义scrollviewer后,无法拖动滚动条。

它适用于具有列表框的其他用户控件。

只有用户控件之间的区别是WrapPanel

    <!--ListBoxItem Style-->
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
    <Setter
        Property="FocusVisualStyle"
        Value="{x:Null}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border
                    x:Name="ItemBorder"
                    BorderBrush="Transparent"
                    Background="Transparent"
                    BorderThickness="1"  Margin="15"                                                                                           
                    >
                    <ContentPresenter/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger
                            Property="IsSelected"
                            Value="True">
                        <Setter
                            TargetName="ItemBorder"
                            Property="Background"
                            Value="{StaticResource G2Brush}"/>
                        <Setter
                            TargetName="ItemBorder"
                            Property="BorderBrush"
                            Value="{StaticResource G4Brush}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<!--ListBox Style-->
<Style TargetType="{x:Type ListBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <ScrollViewer x:Name="ScrollViewer">
                    <ItemsPresenter/>
                </ScrollViewer>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Border x:Name="border"  Background="Transparent" Margin="2">
                    <Grid Width="70" Height="70">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="10"/>
                        </Grid.RowDefinitions>
                        <Border x:Name="borderImage" Background="Transparent" Grid.Row="0">
                            <Image Source="{Binding Path=FilePath}" />
                        </Border>

                        <TextBlock x:Name="ThumbFileName" Text="{Binding Path=FileName}" Grid.Row="1" 
                                    Style="{StaticResource ThumbFileName}" VerticalAlignment="Top" HorizontalAlignment="Left"
                                    TextTrimming="CharacterEllipsis"                                              
                                    />
                    </Grid>
                </Border>
                <DataTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="borderImage" Property="BorderBrush" Value="{StaticResource B1Brush}" />
                        <Setter TargetName="borderImage" Property="BorderThickness" Value="1" />
                        <Setter TargetName="ThumbFileName" Property="Foreground" Value="{StaticResource B1Brush}" />
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate >
                <WrapPanel Margin="10" 
                            Background="Red"  
                            />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter
        Property="ScrollViewer.HorizontalScrollBarVisibility"
        Value="Disabled"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
</Style>

    <Grid Background="Transparent">
            <ListBox x:Name="reportListViewControl"  
                     Background="Transparent" 
                     Foreground="{StaticResource G4Brush}" 
                     BorderThickness="0"
                     Style="{StaticResource ListBoxStyle}"
                     ItemContainerStyle="{StaticResource ListBoxItemStyle}"
                     Drop="reportListViewControl_Drop"
                     SelectionMode="Extended"                 
                     SelectionChanged="reportListViewControl_SelectionChanged"
                     AllowDrop="True">
            </ListBox>
        </Grid>

1 个答案:

答案 0 :(得分:0)

在您的示例中,WrapPanel占用了显示其项目所需的所有空间。要启用滚动条,您可以按其父级ListBox

的大小限制它
<ItemsPanelTemplate>
    <WrapPanel Margin="10" Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"
               Height="{Binding ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"/>
</ItemsPanelTemplate>

或者您可以使用UniformGrid并使用ColumnsRows属性来控制它。