在这里扩展答案,不要将StrokeThickness 与BorderThickness 混淆。 FrameworkElement 继承层次有不同的分支
-
Shape 包括此处讨论的 UI 就绪 Rectangle 类与...
-
Decorator 具有 Child 属性的框架元素,其中框架具有厚度,例如Border ...
-
Control,通常带有Content的一些概念,其中一些也可能从Decorator模仿或暴露自己的BorderThickness/BorderBrush属性
就当前问题而言,修改任何FrameworkElement 上的StrokeThickness 或BorderThickness 只能在以下情况下影响其位置:
-
元素本身首先决定更改会影响其自身的整体大小。如果更改不影响其自身的实际大小,则其位置无法移动。但是如果元素的
DesiredSize属性确实发生了变化,那么这个变化会被元素的布局父级注意到,给出第二个条件......
-
...还有,如果由于
DesiredSize 更改,布局父级决定移动元素。
因此,所提出问题的正确答案是“可能”,因为它取决于所讨论的特定元素(在 OP 的情况下为 Rectangle)以及布局父级它是附加的。
对于 OP 的特定示例,显式设置 Rectangle 上的 Width 和 Height 属性被解释为请求最终的总体大小,这将包括其 Stroke 的完整外部范围。这种模式的效果是总是减少内部区域,因为 Stroke 总是会从整体尺寸的内部中取出,或者“强制进入”,因为它对于最终的DesiredSize更重要同意Width/Height。
此外,我相信Rectangle 永远不会“增大”它的尺寸,以满足过度肥胖的StrokeThickness 填充形状的整个内部。因此,由于 Rectangle 永远无法满足条件 (1.),因此另一个条件没有实际意义,因此 在 Rectangle 上更改 StrokeThickness 永远不会导致它移动。
我修改了@Sheridan 提供的代码以比较Shape 派生元素Rectangle 的使用——它不包含任何布局子元素(左列)——与@ 987654354@-派生元素Border,包含TextBlock 作为报告内部可用大小的子元素(右列)。您可以看到,随着StrokeThickness 的变化,Rectangle 本身的ActualWidth/ActualHeight 不会发生变化,但TextBlock 包含在 Border 元素中的情况并非如此。
这是实验代码,以防有人想进一步使用它:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!-- Insert Grid.Resources (shown separately below) here -->
<Rectangle x:Name="r1" Grid.Column="0" Grid.Row="0" StrokeThickness="1" />
<Label Grid.Column="0" Grid.Row="0" Target="{x:Reference r1}" />
<Rectangle x:Name="r2" Grid.Column="0" Grid.Row="1" StrokeThickness="5" />
<Label Grid.Column="0" Grid.Row="1" Target="{x:Reference r2}" />
<Rectangle x:Name="r3" Grid.Column="0" Grid.Row="2" StrokeThickness="10" />
<Label Grid.Column="0" Grid.Row="2" Target="{x:Reference r3}" />
<Rectangle x:Name="r4" Grid.Column="0" Grid.Row="3" StrokeThickness="15" />
<Label Grid.Column="0" Grid.Row="3" Target="{x:Reference r4}" />
<Border Grid.Column="1" Grid.Row="0" BorderThickness="1" Child="{StaticResource dt}" />
<Border Grid.Column="1" Grid.Row="1" BorderThickness="5" Child="{StaticResource dt}" />
<Border Grid.Column="1" Grid.Row="2" BorderThickness="10" Child="{StaticResource dt}" />
<Border Grid.Column="1" Grid.Row="3" BorderThickness="15" Child="{StaticResource dt}" />
</Grid>
<Grid.Resources>
<TextBlock x:Key="dt"
x:Shared="False"
Foreground="White"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Run Text="{Binding Path=DataContext.ActualWidth,StringFormat='ActualWidth: {0}',Mode=OneWay}" />
<LineBreak />
<Run Text="{Binding Path=DataContext.ActualHeight,StringFormat='ActualHeight: {0}',Mode=OneWay}" />
</TextBlock>
<Style TargetType="Label">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<TextBlock Foreground="White"
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Width="{Binding Path=Target.ActualWidth}"
Height="{Binding Path=Target.ActualHeight}">
<Run Text="{Binding Path=Target.ActualWidth,StringFormat='ActualWidth: {0}',Mode=OneWay}" />
<LineBreak />
<Run Text="{Binding Path=Target.ActualHeight,StringFormat='ActualHeight: {0}',Mode=OneWay}" />
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="common_style" TargetType="FrameworkElement">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Width" Value="150" />
<Setter Property="Height" Value="80" />
</Style>
<Style TargetType="Border" BasedOn="{StaticResource common_style}">
<Setter Property="BorderBrush" Value="Navy" />
<Setter Property="Background" Value="Gray" />
</Style>
<Style TargetType="Rectangle" BasedOn="{StaticResource common_style}">
<Setter Property="Stroke" Value="Navy" />
<Setter Property="Fill" Value="Gray" />
</Style>
</Grid.Resources>