如何制作圆角网格?

问题描述 投票:13回答:2

我试过,它在网格边框上方设置了一个新边框:

<Window x:Class="Class.Window"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Width="379" Loaded="Window_Loaded"
        AllowsTransparency="True"
        ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True" WindowStyle="None" Height="110">    
    <Border BorderBrush="Black" BorderThickness="1,1,1,1" CornerRadius="30,30,30,30">
        <Grid>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="62,12,0,0" Name="textBlock_From" Text="" VerticalAlignment="Top" Width="283" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="62,38,0,0" Name="textBlock_Subject" Text="" VerticalAlignment="Top" Width="283" 
                       MouseLeftButtonDown="textBlock_Subject_MouseLeftButtonDown" MouseEnter="textBlock_Subject_MouseEnter" MouseLeave="textBlock_Subject_MouseLeave" />
        </Grid>
    </Border>
</Window>
wpf border rounded-corners
2个回答
30
投票

由于目前还不完全清楚你想要做什么,我想你想要一个圆角和透明背景的窗户。你的解决方案是正确的,你只需要设置Window背景透明度和Border的背景。

<Window x:Class="Class.Window"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Width="379" Loaded="Window_Loaded"
    AllowsTransparency="True"
    ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True" WindowStyle="None" Height="110" Background="Transparent">    
    <Border Background="White" BorderBrush="Black" BorderThickness="1,1,1,1" CornerRadius="30,30,30,30">
        <Grid>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="62,12,0,0" Name="textBlock_From" Text="" VerticalAlignment="Top" Width="283" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="62,38,0,0" Name="textBlock_Subject" Text="" VerticalAlignment="Top" Width="283" 
                   MouseLeftButtonDown="textBlock_Subject_MouseLeftButtonDown" MouseEnter="textBlock_Subject_MouseEnter" MouseLeave="textBlock_Subject_MouseLeave" />
        </Grid>
    </Border>
</Window>

1
投票

更好的解决方案是如何在XAML中定义边界和网格顺序。例如,此方案用于屏蔽圆形边框下方的方形网格角,而主网格是透明的:

<Window x:Class="Class.Window"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Width="379" Loaded="Window_Loaded"
    AllowsTransparency="True"
    ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True" WindowStyle="None" 
    Height="110" Background="Transparent">    

    <Grid Background="Transparent">
        <Grid Background="White" Margin="4">
            <!-- ...place functional control elements here... -->
        </Grid>

        <Border CornerRadius="12,12,12,12" BorderThickness="6" Padding="4">
            <!-- ...set your desired border brush color here... -->
        </Border>
    </Grid>
</Window>
© www.soinside.com 2019 - 2024. All rights reserved.