C# 圆角网格角与窗圆角匹配

C# 圆角网格角与窗圆角匹配,c#,wpf,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" I

我有一个圆角的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”显示在所有主控件上,背景为黑色,不透明度设置为允许主控件具有一定的可视性,但用户始终无法查看它们


我想使网格的角落以及圆角,以便它与窗口的圆角匹配。目前,它们是方形的,并延伸到窗口角的正常方形位置。

我认为这可能是一个很好的转换器候选

将这段代码放在您的代码隐藏中,或者如果需要,将其放在单独的文件中:

public class VisibilityToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        var visible = (Visibility)value;

        return visible == Visibility.Visible
            ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(70, 130, 180))
            : new SolidColorBrush(System.Windows.Media.Color.FromRgb(173, 216, 230));
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
然后在XAML中引用它(删除
):


我认为这可能是一个很好的转换器候选人

将这段代码放在您的代码隐藏中,或者如果需要,将其放在单独的文件中:

public class VisibilityToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        var visible = (Visibility)value;

        return visible == Visibility.Visible
            ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(70, 130, 180))
            : new SolidColorBrush(System.Windows.Media.Color.FromRgb(173, 216, 230));
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
然后在XAML中引用它(删除
):


我建议您用填充物制作一个圆形边框,背景与网格相同

<Border CornerRadius="10" Padding="10" Background=Black>
<Grid x:Name="gdWait" Visibility="Collapsed">
<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>
</Border>

我建议您用填充物制作一个圆形边框,背景与网格相同

<Border CornerRadius="10" Padding="10" Background=Black>
<Grid x:Name="gdWait" Visibility="Collapsed">
<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>
</Border>

试试这个

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"       
    xmlns:hp="clr-namespace:WpfApplication2"
    Title="Portal" Height="301" Width="489" Template="{DynamicResource WindowTemplate}"   WindowStyle="None" WindowStartupLocation="CenterScreen"  ResizeMode="NoResize"  Background="Transparent" AllowsTransparency="True">
<Window.Resources>
    <ControlTemplate x:Key="WindowTemplate">
        <Border BorderBrush="DarkBlue" Background="LightBlue" BorderThickness="2" Margin="5" CornerRadius="20">
            <Border BorderBrush="DarkBlue" Background="#576C73" Margin="5" BorderThickness="2"  CornerRadius="20">
                <Grid VerticalAlignment="Stretch" Background="#576C73" HorizontalAlignment="Stretch" Margin="5">
                    <Grid x:Name="gdWait" Margin="5" Background="#576C73" >
                        <Grid Background="#576C73"/>
                        <Label x:Name="lblStatus" Content="Please Wait"  HorizontalAlignment="Center" HorizontalContentAlignment="Center"  VerticalContentAlignment="Center"  VerticalAlignment="Center" FontWeight="Bold" Foreground="Red" FontSize="24" Margin="28,0,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>
            </Border>
        </Border>
    </ControlTemplate>
</Window.Resources>

我已经更改了窗口模板,它正在处理不同大小的窗口。。抱歉,如果我对您的要求有误。

试试这个

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"       
    xmlns:hp="clr-namespace:WpfApplication2"
    Title="Portal" Height="301" Width="489" Template="{DynamicResource WindowTemplate}"   WindowStyle="None" WindowStartupLocation="CenterScreen"  ResizeMode="NoResize"  Background="Transparent" AllowsTransparency="True">
<Window.Resources>
    <ControlTemplate x:Key="WindowTemplate">
        <Border BorderBrush="DarkBlue" Background="LightBlue" BorderThickness="2" Margin="5" CornerRadius="20">
            <Border BorderBrush="DarkBlue" Background="#576C73" Margin="5" BorderThickness="2"  CornerRadius="20">
                <Grid VerticalAlignment="Stretch" Background="#576C73" HorizontalAlignment="Stretch" Margin="5">
                    <Grid x:Name="gdWait" Margin="5" Background="#576C73" >
                        <Grid Background="#576C73"/>
                        <Label x:Name="lblStatus" Content="Please Wait"  HorizontalAlignment="Center" HorizontalContentAlignment="Center"  VerticalContentAlignment="Center"  VerticalAlignment="Center" FontWeight="Bold" Foreground="Red" FontSize="24" Margin="28,0,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>
            </Border>
        </Border>
    </ControlTemplate>
</Window.Resources>
  <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>


我已经更改了窗口模板,它正在处理不同大小的窗口。。抱歉,如果我对您的要求有误。

请使用边框的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>


此解决方案假定您的windows大小为489 x 301,并且无法调整大小。如果需要可调整大小窗口的解决方案,请使用转换器计算矩形几何体的矩形值

使用边框的剪裁属性,如下所示,以满足您的要求

  <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>


此解决方案假定您的windows大小为489 x 301,并且无法调整大小。如果需要可调整大小窗口的解决方案,请使用转换器计算矩形几何体的矩形值

我使用它是为了达到我的目的。只需更改整个窗口边框的颜色。我有一个二级网格,它覆盖在主网格上,这样后面的控件就不可点击,并且二级网格上的不透明度为0.5,它仍然允许用户在显示进度条/消息时看到褪色的控件。我正在使用它来达到我的目的。只需更改整个窗口边框的颜色。我有一个次网格,它覆盖在主网格上,这样它后面的控件就不可点击,次网格上的不透明度为0.5,它仍然允许用户在显示progressbar/消息时看到褪色的控件。这确实可以让控件保持方形,但在视觉上,圆角看起来不太好。最坏的情况就是最坏的情况。如果没有解决方案,我可能不得不接受。这确实有效,让它们保持方形,但在我看来,圆角看起来不太好。最坏的情况就是最坏的情况,如果没有解决办法,我可能不得不接受