WPF参考ScrollViewer模板的ScrollContentPresenter

发布于 2024-07-22 05:06:39 字数 62 浏览 7 评论 0原文

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

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

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

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

发布评论

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

评论(2

柠檬色的秋千 2024-07-29 05:06:39

如果您希望访问 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>

If you wish to get to the ScrollContentPresenter of a ScrollViewer you can use a 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>
世界等同你 2024-07-29 05:06:39

也许您想从内容向上绑定某些内容,而不是尝试从 ScrollViewer 沿着树向下移动?

类似于:

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

注意:该语法可能不是 100% 正确,这超出了我的想象

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文