如何在wpf中创建切角

发布于 2024-12-08 09:35:11 字数 2539 浏览 0 评论 0原文

我有一些代码

                <Border BorderBrush="Black"  BorderThickness="1" CornerRadius="8" Margin="0,0,5,0">
                    <!--Background="#FDFFC1"-->
                    <Border.Background>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                            <GradientStop Color="#FFFFFFFD" />
                            <GradientStop Color="#FEFD9A" Offset="1" />
                        </LinearGradientBrush>
                    </Border.Background>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Border Grid.Row="0"  CornerRadius="10,10,0,0" Height="22" VerticalAlignment="Top">
                            <Label Foreground="Black" FontSize="10"  Margin="5,0,0,0" HorizontalAlignment="Left" 
                                   VerticalAlignment="Center" Content="Text" />
                        </Border>
                        <Grid Grid.Row="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="5*"/>
                            </Grid.ColumnDefinitions>
                            <Border Grid.Column="0" Margin="5,2,0,2">
                                <Image x:Name="tooltipImage" Width="18" Height="18"                                       
                                   Source=""
                                   HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Border>
                            <Border Grid.Column="1" Margin="5,5,5,5">
                                <TextBlock
                                   Foreground="Black"
                                   Text="Test" FontSize="10"
                                   VerticalAlignment="Center" xml:space="preserve"
                                   HorizontalAlignment="Center"
                                   TextAlignment="Justify" MaxWidth="200"
                                   TextWrapping="Wrap"/>
                            </Border>

对于这个边框,我希望它的右下角就像是用剪刀剪下来的或像一张被剪下来的纸。我想我需要使用路径或一些几何图形,但我不知道具体如何使用。

提前致谢 !

I have some code

                <Border BorderBrush="Black"  BorderThickness="1" CornerRadius="8" Margin="0,0,5,0">
                    <!--Background="#FDFFC1"-->
                    <Border.Background>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                            <GradientStop Color="#FFFFFFFD" />
                            <GradientStop Color="#FEFD9A" Offset="1" />
                        </LinearGradientBrush>
                    </Border.Background>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Border Grid.Row="0"  CornerRadius="10,10,0,0" Height="22" VerticalAlignment="Top">
                            <Label Foreground="Black" FontSize="10"  Margin="5,0,0,0" HorizontalAlignment="Left" 
                                   VerticalAlignment="Center" Content="Text" />
                        </Border>
                        <Grid Grid.Row="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="5*"/>
                            </Grid.ColumnDefinitions>
                            <Border Grid.Column="0" Margin="5,2,0,2">
                                <Image x:Name="tooltipImage" Width="18" Height="18"                                       
                                   Source=""
                                   HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Border>
                            <Border Grid.Column="1" Margin="5,5,5,5">
                                <TextBlock
                                   Foreground="Black"
                                   Text="Test" FontSize="10"
                                   VerticalAlignment="Center" xml:space="preserve"
                                   HorizontalAlignment="Center"
                                   TextAlignment="Justify" MaxWidth="200"
                                   TextWrapping="Wrap"/>
                            </Border>

For this border I want the right bottom corner of it to be like it was cutted with a scissor or like a piece of paper that was cutted. I think I need to use Path or some geometry but I don't know exactly how.

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2024-12-15 09:35:11

您可以像这样使用边框的 Clip 属性:

<Border BorderBrush="Black"
        BorderThickness="1"
        CornerRadius="8"
        Margin="0,0,5,0">
    <Border.Clip>
        <PathGeometry>
            <PathFigure StartPoint="0,0">
                <LineSegment Point="70,0" />
                <LineSegment Point="70,20" />
                <LineSegment Point="50,50" />
                <LineSegment Point="0,70" />
            </PathFigure>
        </PathGeometry>
    </Border.Clip>

不幸的是,必须给出点的坐标,并且它们的大小不会随边框变化。为了克服这个问题,您可能需要向 Border 添加一个 SizeChanged 事件处理程序,如下所示:

<Border BorderBrush="Black"
        BorderThickness="1"
        CornerRadius="8"
        Margin="0,0,5,0" SizeChanged="Border_SizeChanged" >
    <Border.Clip>
        <PathGeometry>
            <PathFigure x:Name="BorderClipFigure" StartPoint="0,0">
                <LineSegment Point="70,0" />
                <LineSegment Point="70,20" />
                <LineSegment Point="50,50" />
                <LineSegment Point="0,70" />
            </PathFigure>
        </PathGeometry>
    </Border.Clip>

并在处理程序中编写代码,使用 Border 的大小来修改 PathFigure 的点:

private void Border_SizeChanged(object sender, SizeChangedEventArgs e)
{
    double cutOff = 20;
    var line = BorderClipFigure.Segments[0] as LineSegment;
    line.Point = new Point(e.NewSize.Width, 0);

    line = BorderClipFigure.Segments[1] as LineSegment;
    line.Point = new Point(e.NewSize.Width, e.NewSize.Height - cutOff);

    line = BorderClipFigure.Segments[2] as LineSegment;
    line.Point = new Point(e.NewSize.Width - cutOff, e.NewSize.Height);

    line = BorderClipFigure.Segments[3] as LineSegment;
    line.Point = new Point(0, e.NewSize.Height);
}

You could use the Clip property of the Border like this:

<Border BorderBrush="Black"
        BorderThickness="1"
        CornerRadius="8"
        Margin="0,0,5,0">
    <Border.Clip>
        <PathGeometry>
            <PathFigure StartPoint="0,0">
                <LineSegment Point="70,0" />
                <LineSegment Point="70,20" />
                <LineSegment Point="50,50" />
                <LineSegment Point="0,70" />
            </PathFigure>
        </PathGeometry>
    </Border.Clip>

Unfortunately the coordinates of the points must be given and they will not size with the Border. To overcome that you might want to add a SizeChanged event handler to the Border like this:

<Border BorderBrush="Black"
        BorderThickness="1"
        CornerRadius="8"
        Margin="0,0,5,0" SizeChanged="Border_SizeChanged" >
    <Border.Clip>
        <PathGeometry>
            <PathFigure x:Name="BorderClipFigure" StartPoint="0,0">
                <LineSegment Point="70,0" />
                <LineSegment Point="70,20" />
                <LineSegment Point="50,50" />
                <LineSegment Point="0,70" />
            </PathFigure>
        </PathGeometry>
    </Border.Clip>

And write code in the handler that uses the size of the Border to modify the points of the PathFigure:

private void Border_SizeChanged(object sender, SizeChangedEventArgs e)
{
    double cutOff = 20;
    var line = BorderClipFigure.Segments[0] as LineSegment;
    line.Point = new Point(e.NewSize.Width, 0);

    line = BorderClipFigure.Segments[1] as LineSegment;
    line.Point = new Point(e.NewSize.Width, e.NewSize.Height - cutOff);

    line = BorderClipFigure.Segments[2] as LineSegment;
    line.Point = new Point(e.NewSize.Width - cutOff, e.NewSize.Height);

    line = BorderClipFigure.Segments[3] as LineSegment;
    line.Point = new Point(0, e.NewSize.Height);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文