我如何在ScrollViewer中粘贴控件

本文关键字:控件 ScrollViewer | 更新日期: 2023-09-27 18:08:47

滚动时是否可以避免移动某些控件?

特别是可以在以下代码示例中粘贴第一行(具有浅蓝色背景):

<Grid>
  <ScrollViewer>
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="20" />
        <RowDefinition />
      </Grid.RowDefinitions>
      <Canvas Background="LightBlue" Grid.Row="0" />
      <Grid  Grid.Row="1" >
        <Canvas Background="Beige" Width="100" Height="800" />
      </Grid>
    </Grid>
  </ScrollViewer>
</Grid>

我如何在ScrollViewer中粘贴控件

也许可以试试这样的东西?

<Grid> 
  <ScrollViewer> 
<Grid Height="800" Width="480" > 
  <Canvas Background="Beige" Width="100" Height="800" /> 
</Grid> 
  </ScrollViewer>
  <Canvas Background="LightBlue" Grid.Row="0" Height="20" VerticalAlignment="Top" /> 
</Grid>

更新:

<Style TargetType="ScrollViewer">
  <Setter Property="HorizontalContentAlignment" Value="Left"/>
  <Setter Property="VerticalContentAlignment" Value="Top"/>
  <Setter Property="VerticalScrollBarVisibility" Value="Visible"/>
  <Setter Property="Padding" Value="4"/>
  <Setter Property="BorderThickness" Value="1"/>
  <Setter Property="BorderBrush">
    <Setter.Value>
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FFA3AEB9" Offset="0"/>
        <GradientStop Color="#FF8399A9" Offset="0.375"/>
        <GradientStop Color="#FF718597" Offset="0.375"/>
        <GradientStop Color="#FF617584" Offset="1"/>
      </LinearGradientBrush>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ScrollViewer">
        <Border BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                CornerRadius="2">
          <Grid Background="{TemplateBinding Background}">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*"/>
              <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition Height="20"/>
              <RowDefinition/>
              <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ScrollContentPresenter x:Name="ScrollContentPresenter"
                                    Cursor="{TemplateBinding Cursor}"
                                    ContentTemplate=
                                      "{TemplateBinding ContentTemplate}"
                                    Margin="{TemplateBinding Padding}"
                                    Grid.Row="1"/>
            <Rectangle Grid.Column="1" Fill="#FFE9EEF4" Grid.Row="2"/>
            <ScrollBar x:Name="VerticalScrollBar"
                       Grid.Column="1"
                       IsTabStop="False"
                       Maximum="{TemplateBinding ScrollableHeight}"
                       Margin="0,-1,-1,-1"
                       Minimum="0"
                       Orientation="Vertical"
                       Grid.Row="0"
                       Visibility="{TemplateBinding
                         ComputedVerticalScrollBarVisibility}"
                       Value="{TemplateBinding VerticalOffset}"
                       ViewportSize="{TemplateBinding ViewportHeight}"
                       Width="18"
                       Grid.RowSpan="3"/>
            <ScrollBar x:Name="HorizontalScrollBar"
                       Grid.Column="0"
                       Height="18"
                       IsTabStop="False"
                       Maximum="{TemplateBinding ScrollableWidth}"
                       Margin="-1,0,-1,-18"
                       Minimum="0"
                       Orientation="Horizontal"
                       Grid.Row="2"
                       Visibility="{TemplateBinding
                         ComputedHorizontalScrollBarVisibility}"
                       Value="{TemplateBinding HorizontalOffset}"
                       ViewportSize="{TemplateBinding ViewportWidth}"/>
            <Rectangle Fill="#FF89B1E2"/>
          </Grid>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

所以,如果你想让垂直滚动条一直走到顶部,你可以为ScrollViewer创建一个样式,将容器网格向下推一点(20px),并用矩形突出显示顶部。

希望这是你想要的…

如果控件在ScrollViewer中,它将滚动。如果你不想让它滚动,它就不应该在ScrollViewer中。Xin的建议,使ScrollViewer的行为像一个"层"或"覆盖"的顶部或下方的Canvas是唯一的方法来保持画布的位置,仍然让它扩展到整个窗口。

至少从你的描述中,听起来你不能轻易或实际上做你想做的事。