为什么我的ScrollViewer不滚动?

时间:2011-02-08 19:21:05

标签: wpf scrollviewer

我的ScrollViewer无效。我创建了一个UserControl来显示箭头指示符,告诉用户滚动查看器可以滚动。所以现在,内容刚刚溢出ScrollViewer。这是我的XAML:

<UserControl x:Class="QCK.Common.ResourceLibrary.CustomControls.ArrowScrollViewer"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Name="UserControl">
    <UserControl.Resources>

        <Style x:Key="ScrollDownArror" TargetType="{x:Type Border}">
            <Setter Property="Margin" Value="2,0,18,0"/>
            <Setter Property="Visibility" Value="Visible"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=CanScrollDown}" Value="True">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="ScrollUpArror" TargetType="{x:Type Border}">
            <Setter Property="Margin" Value="2,0,18,0"/>
            <Setter Property="Visibility" Value="Visible"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=CanScrollUp}" Value="True">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

    </UserControl.Resources>
    <Grid>
        <ScrollViewer Name="scrollViewer" VerticalScrollBarVisibility="Visible">
            <ContentPresenter VerticalAlignment="Top"/>
        </ScrollViewer>

        <Border Style="{StaticResource ScrollUpArror}"  DataContext="{Binding ElementName=c_list}"
                                IsHitTestVisible="false"
                                VerticalAlignment="Top">
            <Image Margin="15" Height="20">
                <Image.Source>
                    <DrawingImage>
                        <DrawingImage.Drawing>
                            <GeometryDrawing Brush="DarkGray" Geometry="M  0, 10 L 50, 30 L 100, 10 Z">
                                <GeometryDrawing.Pen>
                                    <Pen Brush="DimGray" />
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                        </DrawingImage.Drawing>
                    </DrawingImage>
                </Image.Source>
            </Image>
        </Border>

        <Border Style="{StaticResource ScrollDownArror}"  DataContext="{Binding ElementName=c_list}"
                                IsHitTestVisible="false"
                                VerticalAlignment="Bottom">
            <Image Margin="15" Height="20">
                <Image.Source>
                    <DrawingImage>
                        <DrawingImage.Drawing>
                            <GeometryDrawing Brush="DarkGray" Geometry="M  0, 30 L 50, 10 L 100, 30 Z">
                                <GeometryDrawing.Pen>
                                    <Pen Brush="DimGray" />
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                        </DrawingImage.Drawing>
                    </DrawingImage>
                </Image.Source>
            </Image>
        </Border>
    </Grid>
</UserControl>

内容只是在控件内部溢出,没有显示滚动条或允许鼠标滚轮滚动或任何东西。

1 个答案:

答案 0 :(得分:1)

我不是100%确定你希望这个UserControl如何工作所以我可能成为这里的目标。我理解它的方式,你希望能够做这样的事情

<local:ArrowScrollViewer x:Name="userControl11">
    <TextBox Text="Test" AcceptsReturn="True"/>
</local:ArrowScrollViewer>

TextBox将在箭头指标之间结束。在这种情况下,我认为您需要编辑UserControl的模板。试试这个

<UserControl.Resources>
    <Style x:Key="ScrollDownArror" TargetType="{x:Type Border}">
        <Setter Property="Margin" Value="2,0,18,0"/>
        <Setter Property="Visibility" Value="Visible"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=CanScrollDown}" Value="True">
                <Setter Property="Visibility" Value="Visible"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="ScrollUpArror" TargetType="{x:Type Border}">
        <Setter Property="Margin" Value="2,0,18,0"/>
        <Setter Property="Visibility" Value="Visible"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=CanScrollUp}" Value="True">
                <Setter Property="Visibility" Value="Visible"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
<UserControl.Template>
    <ControlTemplate TargetType="{x:Type UserControl}">
        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Border Grid.Row="0" Style="{StaticResource ScrollUpArror}"  DataContext="{Binding ElementName=c_list}"
                                    IsHitTestVisible="false"
                                    VerticalAlignment="Top">
                    <Image Margin="15" Height="20">
                        <Image.Source>
                            <DrawingImage>
                                <DrawingImage.Drawing>
                                    <GeometryDrawing Brush="DarkGray" Geometry="M  0, 10 L 50, 30 L 100, 10 Z">
                                        <GeometryDrawing.Pen>
                                            <Pen Brush="DimGray" />
                                        </GeometryDrawing.Pen>
                                    </GeometryDrawing>
                                </DrawingImage.Drawing>
                            </DrawingImage>
                        </Image.Source>
                    </Image>
                </Border>
                <ScrollViewer Grid.Row="1" Name="scrollViewer" VerticalScrollBarVisibility="Visible">
                    <ContentPresenter Grid.Row="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </ScrollViewer>
                <Border Grid.Row="2" Style="{StaticResource ScrollDownArror}"  DataContext="{Binding ElementName=c_list}"
                                    IsHitTestVisible="false"
                                    VerticalAlignment="Bottom">
                    <Image Margin="15" Height="20">
                        <Image.Source>
                            <DrawingImage>
                                <DrawingImage.Drawing>
                                    <GeometryDrawing Brush="DarkGray" Geometry="M  0, 30 L 50, 10 L 100, 30 Z">
                                        <GeometryDrawing.Pen>
                                            <Pen Brush="DimGray" />
                                        </GeometryDrawing.Pen>
                                    </GeometryDrawing>
                                </DrawingImage.Drawing>
                            </DrawingImage>
                        </Image.Source>
                    </Image>
                </Border>
            </Grid>                
        </Border>
    </ControlTemplate>
</UserControl.Template>

<强>更新

您可以通过多种方式从代码中访问模板中的ScrollViewer。这是我想到的三种方式

  • Loaded
  • ScrollViewer事件添加事件处理程序
  • 在代码隐藏
  • 中使用Template.FindName
  • 遍历Visual Tree

以下是使用Loaded事件的示例

<ScrollViewer Loaded="scrollViewer_Loaded"
              Grid.Row="1"
              ...>

代码

private ScrollViewer m_scrollViewer;
private void scrollViewer_Loaded(object sender, RoutedEventArgs e)
{
    m_scrollViewer = sender as ScrollViewer;
}

如果您想使用FindName

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    m_scrollViewer = this.Template.FindName("scrollViewer", this) as ScrollViewer;
}