【问题标题】:C# WPF - Rounded background colorC# WPF - 圆角背景色
【发布时间】:2021-11-10 13:22:39
【问题描述】:

我怎样才能使红色角落变圆? 我只想设置 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>

【问题讨论】:

  • 是的,类似的,但我尝试了那个解决方案,但我无法让它工作
  • 尽量接近这个例子。您需要一个边框,在此边框内有一个网格,在此网格内您的不透明蒙版边框带有名称(以及半径)以及定义不透明蒙版的网格。不要试图缩短一些东西
  • 将 ClipToBound="True" 添加到 MainBorder 属性

标签: c# wpf border rounded-corners


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    发生这种情况的原因是,虽然您的主边框设置了 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>
    

    【讨论】:

      猜你喜欢
      • 2010-10-03
      • 2013-04-28
      • 2011-12-02
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多