對我來說,這真的看起來像一個奇怪的錯誤,除非有人對此行爲有更好的解釋。
ScrollViewer
(PART_ContentHost)內部使用Template
像:
<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>
感興趣的是:
<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}" />
我們解決您的問題,您可以只設置有以0保證金,而不是{TemplateBinding Padding}
,你會得到你想要的輸出。
但是爲什麼我們需要這樣做呢?
TemplateBinding Padding
似乎被忽略了上直接設置ScrollViewer
這是在內部範圍,並挑選繼承自父填充值的值(Button
),這是15
確定這很奇怪,但更糟糕的是它只適用於Padding。 Foreground
,Background
,Margin
當直接設置在ScrollViewer
上時,它們都會覆蓋TextBox
的字段。我甚至可以確認將Padding
直接使用TextBox
中的使用情況轉移到了默認的樣式設置器,以查看是否有某個優先級案例是問題所在。
它似乎不是。得到相同的輸出。
填充在System.Windows.Controls.Control
中定義,其中前景和背景都是從ScrollViewer
繼承而來的。不知道爲什麼填充本身的行爲有所不同。
我也試圖改變演示者像
<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}" />
它打印的15,15,15,15。不要爲Margin
這樣做。
與{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollViewer}}, Path=Padding}
綁定的效果相同。
我看到一個帖子說ScrollViewer
沒有傳遞給它的孩子設置的屬性。真的不知道,因爲如果是這樣的話,Background
,Margin
和排序怎麼樣都可以超越? Padding
有什麼特別之處?如果它是有效的行爲,我真的不知道如何擺脫這種行爲沒有模板ScrollViewer
以及它是一個令人困惑的實現。
非常感謝您的詳細和經過充分測試的答案!我非常感謝關於scrollviewer模板的輸入和信息。這當然是一個奇怪的例子,這對我來說沒有多大意義。 – 2013-05-07 19:51:16