M. *_*ley 6 xaml path stretch controltemplate
我有ControlTemplate一些Paths.我希望Paths伸展并填充它们所处的控制,例如a Button.我怎样才能做到这一点?
我目前看起来像这样:
<ControlTemplate x:Key="SomeTemplate" TargetType="Button">
<Canvas Background="AliceBlue">
<Path Data="M 99.5,50 A 49.5,49.5 0 1 1 0.5,50 A 49.5,49.5 0 1 1 99.5,50 z"
Fill="White" Stroke="Black" StrokeThickness="1" />
<Path Data="M 15,50 C 17.5,22.5 47.5,22.5 50,50 C 52.5,77.5 82.5,77.5 85,50"
Stroke="Black" StrokeThickness="1" />
</Canvas>
</ControlTemplate>
...
<Button Template="{StaticResource SomeTemplate}" Height="120" Width="120" />
Run Code Online (Sandbox Code Playgroud)
我知道ScaleTransform的StrechX和StretchY属性,但它们只有原来的比例缩放Path的尺寸.
我会使用值转换器吗?或者也许某种形式的相对约束父母的大小?
Bry*_*son 11
在示例中围绕Canvas投掷ViewBox应该可行.
要拉伸路径,请使用该Stretch属性.它就像图像拉伸一样 - 在此处描述:https://msdn.microsoft.com/en-us/library/system.windows.mediaretagech(v = vs10).aspx(System.Windows.Media> Stretch列举).在下面显示的情况下,将值设置为Uniform将保留路径宽高比,填充它占据的控件,以便整个路径可见.
<Path Stretch="Uniform" Data="..."/>
Run Code Online (Sandbox Code Playgroud)
有一个副作用:以这种方式拉伸路径将"规范化"其数据,即使数据被写入以便所有点都从原点[ 1 ]转换,当拉伸时,转换被省略[ 2 ](希望我能清楚地解释这一点).
例如,当Inkscape中有一个混乱的对象(未转换为原点)而不用担心转换的数据时,可以使用它.
<Grid Width="200" Height="200">
<TextBlock Text="(0,0)" />
<TextBlock Text="(200,200)" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
<Path Stroke="Blue" Stretch="None" Fill="Beige" Data="M 63,50 82,86 107,62 84,65 Z"/>
<Rectangle Stroke="Red" StrokeThickness="1"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
<Grid Width="200" Height="200">
<TextBlock Text="(0,0)" />
<TextBlock Text="(200,200)" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
<Path Stroke="Blue" Stretch="Uniform" Fill="Beige" Data="M 63,50 82,86 107,62 84,65 Z"/>
<Rectangle Stroke="Red" StrokeThickness="1"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)