【问题标题】:wpf ClipToBounds right and bottomwpf ClipToBounds 右下角
【发布时间】:2014-10-14 11:32:29
【问题描述】:

如何剪辑路径描边?对于ClipToBounds="True",右下角有不需要的部分。

<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>

编辑

我发现我不需要缩放边框厚度,所以解决方案是:

<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>

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    如果不缩放笔画粗细也可以,你可以放下Viewbox,直接在Path处设置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 中使用路径,例如一个矩形(需要明确设置一些大小):

    <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 封闭。

    【讨论】:

    • 不是我想要的,但我找到了解决方案here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2011-10-04
    • 2020-11-14
    • 2013-12-22
    • 2013-11-09
    • 2021-08-21
    • 1970-01-01
    相关资源
    最近更新 更多