Skip to main content
 首页 » 编程设计

c#之圆角网格角匹配窗口圆角

2024年02月13日31虾米哥

我有一个带圆角的 WPF 窗口

<Window x:Name="windowPortal" x:Class="ICS2GO.PortalWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Portal" Height="301" Width="489" Icon="/Resources/icon.ico"  
    WindowStyle="None" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" Closing="Window_Closing" Background="Transparent" AllowsTransparency="True"> 
 
<Border Name="windowBorder" BorderThickness="2" BorderBrush="DarkBlue"  
    CornerRadius="20" Background="LightBlue" Margin="0,0,0,0"> 
 
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> 
        //main controls, buttons, images ...etc here 
 
        <Grid x:Name="gdWait" > 
            <Grid Background="Black" Opacity="0.5"/> 
            <Label x:Name="lblStatus" Content="Please Wait"  
                HorizontalAlignment="Center" HorizontalContentAlignment="Center"  
                VerticalContentAlignment="Center"  VerticalAlignment="Center"  
                FontWeight="Bold" Foreground="Red" FontSize="24" Margin="28,51,28,62"  
                Height="72" Width="410"/> 
 
            <ProgressBar x:Name="pbWaiting" HorizontalAlignment="Left" Height="30" 
                Margin="110,108,0,0" VerticalAlignment="Top" Width="243" 
                IsIndeterminate="True" Orientation="Horizontal"/> 
        </Grid> 
   </Grid> 

Grid x:Name="gbWait"以黑色背景和不透明度设置显示在所有主要控件上,以允许主要控件的一些可见性,但始终使它们无法被用户点击

我想使 Grid gbWait 的角也变圆,以便与 Window 的圆角相匹配。目前它们是正方形的,并延伸到正常正方形的窗口角落。

请您参考如下方法:

如下使用 Border 的 Clip 属性来实现您的要求。

  <Border Name="windowBorder" BorderThickness="2" BorderBrush="DarkBlue"  
        CornerRadius="20" Background="LightBlue" Margin="0,0,0,0"> 
        <Border.Clip> 
            <RectangleGeometry RadiusX="20" RadiusY="20" Rect="0,0,489,301" > 
            </RectangleGeometry> 
        </Border.Clip>       
       <Grid></Grid>     
  </Border> 

此解决方案假定您的窗口大小为 489 x 301 并且不可调整大小。如果您需要可调整大小窗口的解决方案,请使用转换器来计算 RectangleGeometry 的 Rect 值。