views:

923

answers:

2

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

A: 

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>
rmoore
This is not quite what I'm looking for. What I need to do is get a reference to the ScrollContentPresenter of an existing ScrollViewer. Basically I am needing to get the ActualWidth of this ScrollContentPresenter bc I need a measurement to the inside bounds of the ScrollBar. What I would like to do is get a reference to the ScrollContentPresenter through (C#)code. Maybe I can do this with the VisualTreeHelper, or somehow find the ScrollContentPresenter through the template of the ScrollViewer.
BrandonS
A: 

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

Paul Betts