如何制作圆角网格?

时间:2011-04-18 07:53:14

标签: wpf border rounded-corners

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

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

2 个答案:

答案 0 :(得分:29)

由于目前还不完全清楚你想要做什么,我猜你想要一个圆角和透明背景的窗户。您的解决方案是正确的,您只需设置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 :(得分: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>