【问题标题】:Rounded Corners in UserControl showing inner square borderUserControl 中的圆角显示内部方形边框
【发布时间】:2016-03-26 16:48:30
【问题描述】:

我有一个 WPF 应用程序。

它有一个填满整个页面的网格,共有 3 行。

在中间行,我根据用户选择的菜单按钮显示一个用户控件。

我想在每个用户控件周围创建一个圆形边框,在谷歌搜索后我找到了一个示例并实现了它。

它可以工作,但我得到了内部矩形边框以及圆形外部边框。

这是我的 UserControl 中的标记:

<Border BorderThickness="3" BorderBrush="White" CornerRadius="10" Padding="2"
    HorizontalAlignment="Center" VerticalAlignment="Center">
    <Grid>
        <Grid Background="White" >
            <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="25" />
            <RowDefinition Height="25" />
            <RowDefinition Height="25" />
            <RowDefinition Height="25" />
            <RowDefinition Height="50" />
        </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="220" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
            <Label Grid.Row="0" Grid.Column="1" Content="Search for Customer" />
            <Label Grid.Row="1" Grid.Column="1" Content="Enter Customer First Name"/>
            <Label Grid.Row="3" Grid.Column="1" Content="Enter Customer Last Name" />
            <TextBox Name="txtForeName" Grid.Column="1" Grid.Row="2" />
            <TextBox Name="txtSurname" Grid.Column="1" Grid.Row="4" />
            <Button Name="btnCustomerSearch" Grid.Column="1" Grid.Row="5" />
        </Grid>
    </Grid>
</Border>

它给了我这样的外观:

【问题讨论】:

标签: wpf user-controls rounded-corners


【解决方案1】:

内部矩形边框根本不是边框。里面有边框(白色)和网格(也有白色)。网格适合您的边框,但它们之间的区域没有任何颜色,因此它是透明的,因此您看到它是蓝色的。如果您想要整个区域为白色,请将边框的背景设置为白色。此外,在您的代码中,其中一个嵌套网格看起来是多余的。

编辑:看起来在设置边框的背景后你仍然有两条边框线。只需删除 BorderThickness 和 BorderBrush (与设置它们的默认值相同)并增加一点填充。另外,网格的背景不需要单独设置,从边框开始就已经是白色了。

这是我的想象:

<Border CornerRadius="10" Padding="5" Background="White"
HorizontalAlignment="Center" VerticalAlignment="Center">
    <Grid>
        <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition Height="25" />
        <RowDefinition Height="25" />
        <RowDefinition Height="25" />
        <RowDefinition Height="25" />
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="220" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="1" Content="Search for Customer" />
        <Label Grid.Row="1" Grid.Column="1" Content="Enter Customer First Name"/>
        <Label Grid.Row="3" Grid.Column="1" Content="Enter Customer Last Name" />
        <TextBox Name="txtForeName" Grid.Column="1" Grid.Row="2" />
        <TextBox Name="txtSurname" Grid.Column="1" Grid.Row="4" />
        <Button Name="btnCustomerSearch" Grid.Column="1" Grid.Row="5" />
    </Grid>
</Border>

【讨论】:

  • 您好,感谢您的回答。我回来后会尽快检查
  • 很抱歉我的回复迟到了。虽然您的回答者对我很有意义,但我已经实施了您的建议,但没有奏效
  • @AndrewSimpson 我修复并扩展了我的答案,这次我自己检查了它,所以你可以试试。
  • 嗨,这不起作用。我将删除这个问题并改写它。 stackoverflow.com/questions/34402710/…
【解决方案2】:
  1. 我已更改边框位置,并将其边距设置为 -3。

  2. 将边框背景设置为白色仍会使网格的 4 个角保持可见。

看看这是否适合你。

     <Grid Canvas.Left="40" Canvas.Top="103">
            <Grid Background="White">
                <Grid Margin="5">
                    <Grid Background="White">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="50" />
                            <RowDefinition Height="25" />
                            <RowDefinition Height="25" />
                            <RowDefinition Height="25" />
                            <RowDefinition Height="25" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="220" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Label Grid.Row="0" Grid.Column="1" Content="Search for Customer" />
                        <Label Grid.Row="1" Grid.Column="1" Content="Enter Customer First Name"/>
                        <Label Grid.Row="3" Grid.Column="1" Content="Enter Customer Last Name" />
                        <TextBox Name="txtForeName" Grid.Column="1" Grid.Row="2" />
                        <TextBox Name="txtSurname" Grid.Column="1" Grid.Row="4" />
                        <Button Name="btnCustomerSearch" Grid.Column="1" Grid.Row="5" Content="Press" />
                    </Grid>
                </Grid>
            </Grid>
            <Border BorderThickness="3" BorderBrush="White" CornerRadius="10" Padding="2" Margin="-3"/>
        </Grid>

【讨论】:

  • 谢谢,我回来后会看看哪个答案最适合我
  • 你好,那没用。此网格位于一个 userControl 中,该 userControl 位于父页面的网格内。这是我认为它不起作用的唯一原因?
猜你喜欢
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 2011-09-24
  • 1970-01-01
  • 2017-12-30
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多