Skip to main content
 首页 » 编程设计

c#之网格和图像重叠边框

2024年02月23日15sky-heaven

您好,我正在尝试将图像的角变圆,或者将我的边框重叠到图像的顶部,但我尝试的任何操作似乎都不起作用。

<Window x:Class="WpfApplication1.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        WindowStyle="None" 
        ResizeMode="NoResize" 
        AllowsTransparency="True" 
        WindowStartupLocation="CenterScreen" 
        BorderThickness="0,0,0,0" 
        Background="Transparent" 
        Title="MainWindow" Loaded="Window_Loaded" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="581" d:DesignWidth="733" SizeToContent="WidthAndHeight"> 
    <Border BorderThickness="6" BorderBrush="Black" CornerRadius="12" Padding="0.5" 
        HorizontalAlignment="Center" VerticalAlignment="Center"> 
        <Grid> 
 
            <Image Height="543" HorizontalAlignment="Left" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="711" Source="/WpfApplication1;component/Images/Login.jpg" ImageFailed="image1_ImageFailed" /> 
 
            <TextBox Height="38" HorizontalAlignment="Left" Margin="205,177,0,0" Name="textBox1" VerticalAlignment="Top" Width="299" Background="#00000000" BorderBrush="#00000000" BorderThickness="0" Text="Please enter your email address." FontSize="18" Foreground="White" TextChanged="textBox1_TextChanged" /> 
            <TextBox Background="#00000000" BorderBrush="#00000000" BorderThickness="0" FontSize="18" Foreground="White" Height="38" HorizontalAlignment="Left" Margin="205,256,0,0" Name="textBox2" Text="Please enter your password" VerticalAlignment="Top" Width="299" /> 
        </Grid> 
 
 
    </Border> 
</Window> 

是否可以在网格上重叠边框?

你可以从这个屏幕转储中明白我的意思:

请您参考如下方法:

为清晰起见更新了 XAML 并添加了一些评论:

Grid 的边界正确,带有圆角。这里真正的问题应该是:

Is it possible to overlap a Grid Border on the elements contained in the Grid?

据我所知,这是不可能的。如果您希望 Grid 中包含的所有内容都被裁剪到 Border 的圆角半径,您需要将 Clip 应用到Grid 这样任何包含的元素都不会与边框重叠。

    <Image  
        HorizontalAlignment="Center"  
        Margin="10"  
        Name="image2"  
        Stretch="None"  
        VerticalAlignment="Bottom"  
        Source="/test;component/login_btn.jpg"> <!-- Make sure it's not 
                                                     a self closing tag 
                                                     ending in "/>" --> 
        <Image.Clip>  <!-- Image.Clip needs to be enclosed between 
                           the Image opening tag (above) and 
                           the Image closing tag (2 lines below). --> 
            <RectangleGeometry RadiusX="18" RadiusY="18" Rect="0,0,103,30" /> 
        </Image.Clip> 
    </Image>  <!-- Close the Image tag here. --> 

图像的裁剪区域被定义为带有圆角的 RectangleGeometry。您可以调整属性以匹配您的按钮图像。

这是我的测试截图,其中一张保持原样,另一张剪裁到几何体上:

希望这对您有所帮助。