wpf ClipToBounds正确和底部

时间:2014-10-14 11:32:29

标签: wpf xaml

如何剪辑路径笔划?对于ClipToBounds="True",在rigth和bottom side有不需要的部分。

alt

<Grid  Background="Yellow" ClipToBounds="True">
    <Viewbox Stretch="Fill">
        <Path Data="M30,0 L0,10 L0,40 L30,50 L30,0" Stroke="Red" StrokeThickness="5" />
    </Viewbox>
</Grid>

修改

我发现我只是不需要缩放边框厚度,因此解决方案将是:

enter image description here

<Grid x:Name="grid" Grid.Row="2" Background="Yellow" >
    <Grid.Resources>
        <ScaleTransform x:Key="transform"
                ScaleX="{Binding ActualWidth, ElementName=grid}"
                ScaleY="{Binding ActualHeight, ElementName=grid}" />
    </Grid.Resources>
    <Path Stroke="Red" StrokeThickness="15" Stretch="Fill">
        <Path.Data>
            <PathGeometry Transform="{StaticResource transform}">
                <PathGeometry.Figures>
                    <PathFigureCollection>
                        <PathFigure IsClosed="True" StartPoint="0,0.7">
                            <PathFigure.Segments>
                                <PathSegmentCollection>
                                    <LineSegment Point="1,1" />
                                    <LineSegment Point="1,0" />
                                    <LineSegment Point="0,0.3" />
                                </PathSegmentCollection>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathFigureCollection>
                </PathGeometry.Figures>
            </PathGeometry>
        </Path.Data>
    </Path>
</Grid>

1 个答案:

答案 0 :(得分:0)

如果可以不缩放笔触粗细,可以放弃Viewbox并直接在路径上设置Stretch="Fill"

<Grid Background="Yellow" ClipToBounds="True" Margin="20">
    <Path Stretch="Fill" Data="M30,0 L0,10 L0,40 L30,50 L30,0 Z"
          Stroke="Red" StrokeThickness="20" />
</Grid>

否则,您可以使用VisualBrush中的路径,例如一个Rectangle(需要明确设置一些大小):

<Grid Background="Yellow" ClipToBounds="True" Margin="20">
    <Viewbox Stretch="Fill">
        <Rectangle Width="1" Height="1">
            <Rectangle.Fill>
                <VisualBrush>
                    <VisualBrush.Visual>
                        <Path Data="M30,0 L0,10 L0,40 L30,50 L30,0 Z"
                              Stroke="R*emphasized text*ed" StrokeThickness="5" />
                    </VisualBrush.Visual>
                </VisualBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Viewbox>
</Grid>

另请注意,路径几何图形由尾部Z关闭。