如何在WPF中为用户控件设置透明度?

4
我创建了一个消息框用户控件,圆角如下图所示,但我无法为这个用户控件设置透明。请帮帮我。
非常感谢。
更新:
我按照您的指示进行了实现,但它仍然不透明,我认为背景的用户控件是白色的,请帮助我。以下是我的代码:
<UserControl x:Class="Nanote.TestDialog"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             d:DesignHeight="200"
             Height="200" Width="450"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <Border CornerRadius="20" BorderBrush="Black" BorderThickness="10" 
        Background="Transparent">
        <Rectangle Fill="White" Margin="10" />
    </Border>
</UserControl>

请在您的问题中添加一些XAML,这样更容易帮助您。 - Roel van Westerop
你正在使用边框吗? - Pragmateek
@Pragmateek:好的,请看我的代码。 - PeaceInMind
2个回答

6

请看下面的XAML代码:

<Grid Background="Red">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid Margin="20">
        <Border CornerRadius="20" BorderBrush="Black" BorderThickness="10" 
            Background="Transparent">
            <Rectangle Fill="White" />
        </Border>
    </Grid>
    <Grid Grid.Row="1" Margin="20">
        <Rectangle Fill="White" />
        <Border CornerRadius="20" BorderBrush="Black" BorderThickness="10" 
            Background="Transparent" />
    </Grid>
    <Border Grid.Row="2" CornerRadius="20" BorderBrush="Black" BorderThickness="10" 
        Background="White" Margin="20">
        <Rectangle Fill="White" Margin="10" />
    </Border>
</Grid>

这将生成以下输出: enter image description here 希望你现在可以根据这个例子自己解决问题。

我尝试按照你的示例,但它运行不正确。请查看我的更新。 - PeaceInMind
那么将 UserControl.Background 设置为 Transparent 呢? - Sheridan

3

首先将控件的背景设置为透明,然后添加带有半径(CornerRadius="10")的边框,在该边框内放置内容网格(GRID)

 <Border BorderBrush="Black"  BorderThickness="1,1,1,1" Background="White" CornerRadius="10,10,10,10">
 <Grid ></Grid>
 </Border>

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