如何使一个元素看起来像是在边框"下方",但是当边框有圆角时,边框的内容"在"上方?

Tar*_*Tar 7 c# wpf xaml

TL/DR:

如何使一个元素看起来像是在边框"下方",但是当边框有圆角时,边框的内容"在"上方?

阐述:

这就是我想要完成的事情:当鼠标移动到边框时,从左上角绘制一个"窗帘",直到它覆盖整个边框的内容或者可能只是它的3/4,就像这个例子:

在此输入图像描述

这个"幕布"旨在携带额外的控件来更新其盒子内容.

我现在卡住了创建红色路径 - 我不能使它沿着黑色边框完美对齐,所以它看起来像是来自边界下方,但高于其内容.

这是XAML我目前拥有的:

<UserControl x:Class="FamilyTree.GroupBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="100">
    <Grid>
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
            <StackPanel.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Setters>
                        <Setter Property="VerticalAlignment" Value="Center" />
                    </Style.Setters>
                </Style>
            </StackPanel.Resources>

            <Border CornerRadius="5" BorderBrush="Black" BorderThickness="1" VerticalAlignment="Center" ToolTip="{Binding ToolTipString}" Background="White">
                <Grid>
                    <StackPanel Margin="3">
                        <StackPanel.Resources>
                            <Style TargetType="{x:Type Border}">
                                <Setter Property="CornerRadius" Value="3" />
                                <Setter Property="BorderBrush" Value="Blue" />
                                <Setter Property="BorderThickness" Value="0.6" />
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Padding">
                                    <Setter.Value>
                                        <Thickness Right="2" />
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="Margin" Value="0" />
                            </Style>
                        </StackPanel.Resources>
                        <StackPanel Orientation="Horizontal">
                            <Border>
                                <TextBlock Text="Name" />
                            </Border>
                            <TextBlock Text="/"/>
                            <Border>
                                <TextBlock Text="Family" />
                            </Border>
                        </StackPanel>
                        <TextBlock Text="Group"/>
                    </StackPanel>
                    <Path Stroke="Red" Fill="Red" Data="M50,0 L5,0 C1.5,0 0,1.5 0,5 L0,50" StrokeLineJoin="Round" Margin="0.5"/>
                </Grid>
            </Border>
        </StackPanel>
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

Chr*_* W. 1

如果是我的话。我将使用 LinearGradient 动画来填充边框,而不使用路径等其他对象来实现此效果。这只是一个快速而草率的例子。像这样的东西;

我们的故事板控制渐变的起点/终点以从某个角度引入渐变;

<Storyboard x:Key="Curtain">
   <DoubleAnimationUsingKeyFrames 
                       Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Offset)"                                                                          
                       Storyboard.TargetName="border">
      <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
    </DoubleAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames 
                       Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Offset)"                                                                          
                       Storyboard.TargetName="border">
       <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
     </DoubleAnimationUsingKeyFrames>
</Storyboard>
Run Code Online (Sandbox Code Playgroud)

然后是我们的边境;

<Border x:Name="border" BorderBrush="Blue" BorderThickness="1" Height="200" Width="350">
   <Border.Background>
      <LinearGradientBrush EndPoint="0.964,0.992" StartPoint="0.017,0.019">
         <GradientStop Color="#FFF90202" Offset="0"/>
         <GradientStop Offset="0.001"/>
      </LinearGradientBrush>
   </Border.Background>             
</Border>
Run Code Online (Sandbox Code Playgroud)

这样,无论是否设置了 CornerRadius,或者如果您想要变得更奇特,您都可以在任何形状或路径上执行相同的操作,无论其几何形状如何,并且在执行动画时它将均匀地分布该区域。然而,边框需要更高的 z-index 或 DOM 中更低的位置才能在内容的顶部呈现。

您可能想要调整 StartPoint/EndPoint 和 KeyTime 上的值以获得您想要的结果,但这只是一个 60 秒的快速示例来传达这个概念。

希望这有帮助,干杯!