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

标签 c# wpf

我有一个带圆角的 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 值。

关于c# - 圆角网格角匹配窗口圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21034818/

相关文章:

c# - 附加命令

c# - 在没有 dll 的情况下在 2 个项目之间共享代码

c# - `WordprocessingDocument.Open` 后损坏的文件

c# - 返回 HttpResponseMessage 的 Web API 最佳方法

WPF 生成 TextBlock 内联

c# - 使用 Dispatcher.Invoke 从非主线程更改 WPF 控件

c# - 不聚焦,但允许互动?

c# - WPF 中的文本框边框半径样式

c# - 并行运行多个执行任务

c# - 我如何在 XAML WPF 窗口中拥有动态元素