【问题标题】:WPF Reference ScrollContentPresenter of ScrollViewer TemplateScrollViewer 模板的 WPF 参考 ScrollContentPresenter
【发布时间】:2009-06-03 15:55:54
【问题描述】:

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

【问题讨论】:

    标签: wpf templates controls scrollviewer


    【解决方案1】:

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

    【讨论】:

    • 这不是我想要的。我需要做的是获取对现有 ScrollViewer 的 ScrollContentPresenter 的引用。基本上我需要获取此 ScrollContentPresenter 的 ActualWidth,因为我需要测量 ScrollBar 的内部边界。我想做的是通过(C#)代码获取对 ScrollContentPresenter 的引用。也许我可以用 VisualTreeHelper 做到这一点,或者以某种方式通过 ScrollViewer 的模板找到 ScrollContentPresenter。
    【解决方案2】:

    与其尝试从 ScrollViewer 往下走,也许您想从 Content 中绑定一些东西up

    类似:

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

    注意:该语法可能不是 100% 正确,我想不到

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      相关资源
      最近更新 更多