C# WPF - 圆形背景颜色

C# WPF - Rounded background color

如何让红色的角变圆? 我只想设置 MainBorder 的 CornerRadius,而不是第一个和最后一个 InnerBorder

如果我设置 textBlocks 的背景(而不是 InnerBorders),行为是相同的

<Border Name="MainBorder" Background="Transparent" Width="250" Height="250"  BorderBrush="Black" BorderThickness="3" CornerRadius="20" Margin="500,500,0,0">
            <Grid>                                                
                <Grid.RowDefinitions>                               
                    <RowDefinition Height="1*"/>                 
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Border Name="brdInner1" Grid.Row="0" BorderBrush="Black" BorderThickness="0,0,0,0" Background="Transparent">
                    <TextBlock />
                </Border>
                    <Border Name="brdInner2" Grid.Row="1" BorderBrush="Black" BorderThickness="0,3,0,0" Background="Red">
                    <TextBlock />
                </Border>
                    <Border Name="brdInner3" Grid.Row="2" BorderBrush="Black" BorderThickness="0,3,0,0" Background="Red">
                    <TextBlock />
                </Border>
            </Grid>
            </Border>

使用 here 中的 ClippingBorder class,只需更改主边框的类型:

<local:ClippingBorder x:Name="MainBorder" ...>

发生这种情况的原因是,虽然您的主边框设置了 CornerRadius 属性,但子边框仍设置为正方形,但我已将 CornerRadius 添加到您的最后一个边框控件中。

<Border Name="MainBorder" Background="Transparent" Width="250" Height="250"  BorderBrush="Black" BorderThickness="3" CornerRadius="20" Margin="500,500,0,0">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <Border Name="brdInner1" Grid.Row="0" BorderBrush="Black" BorderThickness="0,0,0,0" Background="Transparent">
                <TextBlock />
            </Border>
            <Border Name="brdInner2" Grid.Row="1" BorderBrush="Black" BorderThickness="0,3,0,0" Background="Red">
                <TextBlock />
            </Border>
            <Border Name="brdInner3" Grid.Row="2" BorderBrush="Black" BorderThickness="0,3,0,0" Background="Red" CornerRadius="0,0,20,20">
                <TextBlock />
            </Border>
        </Grid>
    </Border>