如何制作圆角网格?

21

我尝试过,但它在网格边框上方设置了新的边框:

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

1
一个网格本身没有任何边框,所以你可以在内部或外部放置一个边框,就像你所做的那样。由于这个问题并不是很清楚,所以我只能回答:像你所做的那样。 - Markus Hütter
2个回答

38

由于您想要做什么并不是非常清楚,我猜您想要一个圆角和透明背景的窗口。您的解决方案是正确的,您只需要设置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
如何为整个带有圆角的网格创建渐变背景?这仅在边框具有与背景相同的纯色设置时才有效。 - Johncl
Johncl,你说得完全正确,如果在边框中填充渐变,则网格背景也会填充透明度无法正常工作。 - Ashish-BeJovial
当我在网格中设置背景颜色时,它不起作用。我不明白为什么会这样。 - Masuri

2
更好的解决方案在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>


太好了,解决得很棒,谢谢!我遇到的一个问题是边框会阻止下方项目的点击。将IsHitTestVisible="False"添加到边框控件中解决了这个问题。只是想分享一下,以防有人遇到相同的问题。 - Qjimbo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接