为什么我的 ScrollViewer 不滚动?

发布于 2024-10-16 05:28:57 字数 3904 浏览 2 评论 0原文

我的 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>

内容只是在控件内溢出,不显示滚动条或允许鼠标滚轮滚动或任何其他内容。

My ScrollViewer is not working. I've created a UserControl to show arrow indicators that tell the user that the scroll viewer can be scrolled. So now, the content just overflows the ScrollViewer. Here is my 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>

The contents just overflow inside the control not showing a scroll bar or allowing mouse wheel scrolling or anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事灯 2024-10-23 05:28:57

我不是 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。想到的三种方法

  • ScrollViewerLoaded 事件添加事件处理程序
  • 在后面的代码中使用 Template.FindName 遍历
  • 可视化树

下面是使用 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;
}

I'm not 100% sure how you want this UserControl to work so I might be of target here. The way I understand it, you want to be able to do something like this

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

And the TextBox will end up between the Arrow Indicators. In that case I think you'll need to edit the Template of the UserControl instead. Try it like this

<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>

Update

There are several ways which you can access the ScrollViewer in the Template from code behind. Here's three ways that comes to mind

  • Add an event handler for the Loaded event of the ScrollViewer
  • Use Template.FindName in code behind
  • Traverse the Visual Tree

Here's an example using the Loaded event

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

Code behind

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

And if you like to use FindName

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    m_scrollViewer = this.Template.FindName("scrollViewer", this) as ScrollViewer;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文