It really looks like a strange mistake for me if someone has no better explanation for this behavior.
ScrollViewer (PART_ContentHost) internally uses Template as:
<ControlTemplate TargetType="{x:Type ScrollViewer}"> <Grid x:Name="Grid" Background="{TemplateBinding Background}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Rectangle x:Name="Corner" Grid.Row="1" Grid.Column="1" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /> <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.Row="0" Grid.Column="0" Margin="{TemplateBinding Padding}" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" /> <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Row="0" Grid.Column="1" AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" ViewportSize="{TemplateBinding ViewportHeight}" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" /> <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Row="1" Grid.Column="0" AutomationProperties.AutomationId="HorizontalScrollBar" Cursor="Arrow" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" /> </Grid> </ControlTemplate>
Interesting bit:
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.Row="0" Grid.Column="0" Margin="{TemplateBinding Padding}" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" />
Now, to fix your problem, you can simply set it to 0 instead of {TemplateBinding Padding} , and you will get the desired result.
But why do we need this?
TemplateBinding Padding seems to ignore the value set directly to the ScrollViewer , which is located in the inner area, and selects the ScrollViewer value inherited from the parent ( Button ), which is 15.
Well, thatβs weird, but worse, this is just for Padding. Foreground , Background , Margin everything is fine, if they are installed directly on the ScrollViewer , they override the TextBox fields. I even confirmed moving Padding , installed directly in TextBox to use, to the default style installer to see if there was a priority issue.
This does not seem to be the case. Got the same conclusion.
Filling is defined in System.Windows.Controls.Control , which is the same class as Foreground and Background, from this inherited ScrollViewer . Not sure if only indents behave differently.
I also tried changing the presenter to something like
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.Row="0" Grid.Column="0" Margin="{TemplateBinding Margin}" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" Content="{TemplateBinding Padding}" ContentTemplate="{TemplateBinding ContentTemplate}" />
He prints 15,15,15,15. Doesn't do this for Margin .
The same effect with binding {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollViewer}}, Path=Padding} .
I saw one message in which ScrollViewer does not pass on the properties set on it to children. Actually it didnβt work, since if it were like Background , Margin and sortings would be perfect for an excessive ride? What is special about Padding ? If this is a valid behavior, I really don't see how to get rid of this behavior without the Templating ScrollViewer as well, and this is one confusing implementation.