【问题标题】:Rounded Corners in UserControl not showingUserControl 中的圆角未显示
【发布时间】:2016-03-27 22:37:09
【问题描述】:

我正在尝试让我的 UserControl 显示圆角。

这是我的标记:

<Border CornerRadius="10" Padding="5" HorizontalAlignment="Center" VerticalAlignment="Center" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="20" />
            <RowDefinition Height="25" />
            <RowDefinition Height="25" />
            <RowDefinition Height="25" />
            <RowDefinition Height="25" />
            <RowDefinition Height="50" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="10" />
            <ColumnDefinition Width="220" />
            <ColumnDefinition Width="10" />
        </Grid.ColumnDefinitions>
        <Grid.Background>
            <ImageBrush ImageSource="{StaticResource NavyBlueGround}" />
        </Grid.Background>
        <Label Grid.Column="1" Grid.Row="0"  Style="{StaticResource GelTitle}"   Content="Customer Search" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
        <Label Grid.Row="1" Grid.Column="1" Style="{StaticResource GelCaption}"  Content="Enter Customer First Name" VerticalAlignment="Bottom" />
        <TextBox  Grid.Row="2" Grid.Column="1" Name="txtForeName"  Background="White" VerticalContentAlignment="Center"/>
        <Label Grid.Row="3" Grid.Column="1" Style="{StaticResource GelCaption}"  Content="Enter Customer Last Name" VerticalAlignment="Bottom" />
        <TextBox Name="txtSurname" Grid.Column="1" Grid.Row="4"  FontSize="14" Text="hello" VerticalContentAlignment="Center" VerticalAlignment="Stretch" HorizontalContentAlignment="Center" />
        <Button Name="btnCustomerSearch" Grid.Column="1" Grid.Row="5" Style="{StaticResource GelButton}" Content="Search" Click="btnCustomerSearch_Click" VerticalAlignment="Center" />
    </Grid>
</Border>

这就是它的样子:

【问题讨论】:

  • 为边框添加不透明背景,您将开始看到圆角。

标签: wpf user-controls rounded-corners


【解决方案1】:

不要为网格使用背景,而是将其用作边框。

<Border CornerRadius="10" Padding="5" HorizontalAlignment="Center" VerticalAlignment="Center" >
    <Border.Background>
        <ImageBrush ImageSource="{StaticResource NavyBlueGround}" />
    </Border.Background>

编辑:此问题的原因是边框(当 padding=5 时)将其子元素的边距设置为 5 像素。同时定义 CornerRadius 将网格的边距设置得更远。

所以 Grid 的边界(及其背景)不是圆角出现的地方。

【讨论】:

  • 看起来很有希望,我一回来就试试。谢谢
  • 很好的答案,很简单。我承认我仍然习惯 WPF,但我学得越多,我就越喜欢它而不是 WinForms。谢谢
【解决方案2】:

你一定有其他事情发生。我使用 UserControl 将您的代码粘贴到了一个新项目中,并且效果很好。

如果一个新项目对您来说效果不一样,您能否提供更多详细信息,并比较差异以帮助解决问题?

【讨论】:

  • 您好,感谢您在这方面的所有时间。我接受了一个更简单的答案。关于你的理论,我倾向于同意你的观点。我正在为内部元素使用样式,但它们绝不会影响边框,因此它必须只是对“可能”影响外部边框的内部元素使用样式的“事情”。我接受的答案消除了这种“困惑”,非常感谢您的评论/回答
  • 它对你来说很好,因为你还为边框设置了背景:)
猜你喜欢
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 2018-06-23
相关资源
最近更新 更多