WPF参考ScrollViewer模板的ScrollContentPresenter

时间:2021-11-25 13:11:53

I have a WPF ScrollViewer, and I would like to get to the ScrollContentPresenter of it's template.

我有一个WPF ScrollViewer,我想到它的模板的ScrollContentPresenter。

2 个解决方案

#1


If you wish to get to the ScrollContentPresenter of a ScrollViewer you can use a ControlTemplate

如果您希望访问ScrollViewer的ScrollContentPresenter,可以使用ControlTemplate

<ScrollViewer Style="{StaticResource LeftScrollViewer}"/>


<Style x:Key="LeftScrollViewer" TargetType="{x:Type ScrollViewer}">
 <Setter Property="OverridesDefaultStyle" Value="True"/>
 <Setter Property="Template">
 <Setter.Value>
  <ControlTemplate TargetType="{x:Type ScrollViewer}">
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
      </Grid.RowDefinitions>

      <ScrollContentPresenter Grid.Column="1"/>

      <ScrollBar Name="PART_VerticalScrollBar"
        Value="{TemplateBinding VerticalOffset}"
        Maximum="{TemplateBinding ScrollableHeight}"
        ViewportSize="{TemplateBinding ViewportHeight}"
        Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
      <ScrollBar Name="PART_HorizontalScrollBar"
        Orientation="Horizontal"
        Grid.Row="1"
        Grid.Column="1"
        Value="{TemplateBinding HorizontalOffset}"
        Maximum="{TemplateBinding ScrollableWidth}"
        ViewportSize="{TemplateBinding ViewportWidth}"
        Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>

    </Grid>
   </ControlTemplate>
  </Setter.Value>
 </Setter>
</Style>

#2


Instead of trying to go down the tree from ScrollViewer, maybe you want to bind something from the Content up?

而不是试图从ScrollViewer下来树,也许你想从内容绑定一些东西?

Something like:

<Grid Width={Binding Path=ActualWidth RelativeBinding={RelativeBinding FindAncestor, AncestorType={x:Type ScrollContentPresenter}}}>

Note: That syntax might not be 100% right, it's off the top of my head

注意:该语法可能不是100%正确,它是我的头脑

#1


If you wish to get to the ScrollContentPresenter of a ScrollViewer you can use a ControlTemplate

如果您希望访问ScrollViewer的ScrollContentPresenter,可以使用ControlTemplate

<ScrollViewer Style="{StaticResource LeftScrollViewer}"/>


<Style x:Key="LeftScrollViewer" TargetType="{x:Type ScrollViewer}">
 <Setter Property="OverridesDefaultStyle" Value="True"/>
 <Setter Property="Template">
 <Setter.Value>
  <ControlTemplate TargetType="{x:Type ScrollViewer}">
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
      </Grid.RowDefinitions>

      <ScrollContentPresenter Grid.Column="1"/>

      <ScrollBar Name="PART_VerticalScrollBar"
        Value="{TemplateBinding VerticalOffset}"
        Maximum="{TemplateBinding ScrollableHeight}"
        ViewportSize="{TemplateBinding ViewportHeight}"
        Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
      <ScrollBar Name="PART_HorizontalScrollBar"
        Orientation="Horizontal"
        Grid.Row="1"
        Grid.Column="1"
        Value="{TemplateBinding HorizontalOffset}"
        Maximum="{TemplateBinding ScrollableWidth}"
        ViewportSize="{TemplateBinding ViewportWidth}"
        Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>

    </Grid>
   </ControlTemplate>
  </Setter.Value>
 </Setter>
</Style>

#2


Instead of trying to go down the tree from ScrollViewer, maybe you want to bind something from the Content up?

而不是试图从ScrollViewer下来树,也许你想从内容绑定一些东西?

Something like:

<Grid Width={Binding Path=ActualWidth RelativeBinding={RelativeBinding FindAncestor, AncestorType={x:Type ScrollContentPresenter}}}>

Note: That syntax might not be 100% right, it's off the top of my head

注意:该语法可能不是100%正确,它是我的头脑