简体   繁体   English

圆角网格角以匹配窗口圆角

[英]Rounded Grid corners to match Window rounded corners

I have a WPF window with rounded corners 我有一个带有圆角的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" is displayed over all main controls with a black background and opacity set as to allow some visablility of the main controls but alway renders them unclickable by the user 网格x:Name =“ gbWait”显示在所有具有黑色背景和不透明度的主控件上,以使主控件具有一定的可验证性,但始终使用户无法点击它们

I would like to make Grid gbWait's corners rounded as well so it matches with the Window's rounded corners. 我想将Grid gbWait的角也弄圆,使其与Window的圆角匹配。 Currently they are square and extend passed the window corner where it is normal square. 当前它们是正方形,并且延伸通过了正常正方形的窗口角。

Use the Clip property of the Border as follows to achieve your requirement. 如下使用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>

This solution assumes your windows size is 489 x 301 and it is not resizable. 此解决方案假定您的Windows尺寸为489 x 301,并且无法调整大小。 If you need solution for resizable window then use converter to calculate the Rect values of RectangleGeometry. 如果您需要可调整大小的窗口的解决方案,则使用转换器来计算RectangleGeometry的Rect值。

I think this might be a good candidate for a converter. 我认为这可能是转换器的不错选择。

Place this piece of code in your code-behind, or a separate file if you want: 将此代码段放置在您的代码隐藏区中,如果需要,也可以将其放置在单独的文件中:

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();
    }
}

Then reference it in your XAML (remove <Grid Background="Black" Opacity="0.5"/> ): 然后在您的XAML中引用它(删除<Grid Background="Black" Opacity="0.5"/> ):

<Window.Resources>
    <l:VisibilityToBrushConverter x:Key="converter" />
</Window.Resources>
<Grid>
    <Border Name="windowBorder" BorderThickness="2" BorderBrush="SteelBlue" CornerRadius="20"
            Background="{Binding ElementName=gdWait, Path=Visibility, Converter={StaticResource converter}}" Margin="0,0,0,0">
        <Grid x:Name="gdWait" Visibility="Visible">
            <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>
</Grid>

I suggest that you make a rounded border with paddings, and with same background as your Grid 我建议您使用填充和与网格相同的背景来制作圆角边框

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

Try this 尝试这个

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

i have changed the window template ..and it is working on different size of window.. sorry if i am wrong to your requirement. 我已经更改了窗口模板..并且它正在不同尺寸的窗口上工作..对不起,如果我对您的要求不对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM