【问题标题】:WPF borderless application with round corners带圆角的 WPF 无边界应用程序
【发布时间】:2012-07-18 03:54:29
【问题描述】:

我创建了一个无边界的 WPF 应用程序,并且有一张我想作为我的背景的图像。我在 Paint 中创建了我的图像,所以我没有让角落透明的选项。我想让应用程序有圆角,但我没有得到 XAML 的预期结果。我尝试添加边框以获得所需的效果,但是当我运行应用程序时,图像仍然在边框前面。这是我的代码。我做错了什么?

<Border BorderBrush="Black" CornerRadius="15,15,15,15" BorderThickness="1,1,1,1" Width="242" Height="426">
    <Grid>
        <Image Height="425" Name="image1" Stretch="Fill" Width="240" Source="/Test;component/Test.png" />
        <Grid Height="334" HorizontalAlignment="Left" Margin="24,39,0,0" Name="grid1" VerticalAlignment="Top" Width="198" />
    </Grid>
</Border>

【问题讨论】:

  • 我最终解决这个问题的方法是在paintshop中编辑照片以使角落透明..一旦完成,我删除了边框和上面的代码工作。感谢 erez 和 andrew 的时间和帮助,但我无法从您的回答中得到预期的结果
  • 我已经更新了我的答案。试试看。

标签: wpf xaml border


【解决方案1】:

窗口中的这些设置将使其透明:

WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"

然后只需将边框的背景设置为图像:

  <Border BorderBrush="Transparent" BorderThickness="1" CornerRadius="15">
            <Border.Background>
                <ImageBrush>
                    <ImageBrush.ImageSource>
                <BitmapImage UriSource="/Test;component/Test.png" />
                    </ImageBrush.ImageSource>
                </ImageBrush>
            </Border.Background>
        </Border>

【讨论】:

  • 嗯,不知道为什么我想要一个椭圆……我有一个矩形图像,我想要圆角。我不想有一个椭圆。
  • poco 我已经更新了我的答案,它是带边框而不是椭圆
  • 我试过了,但边框仍然放在图像后面......我能够开始工作的唯一解决方案是我上面列出的那个。
【解决方案2】:

我认为您需要将 ClipToBounds="True" 添加到图像中。

【讨论】:

    【解决方案3】:

    这是一个特殊的边框,ClipToBounds 可以完美地工作。如果您将 Erez 的回答中的“Border”更改为“ClippingBorder”,那么它应该可以工作。

    ''' <Remarks>
    '''     As a side effect ClippingBorder will surpress any databinding or animation of 
    '''         its childs UIElement.Clip property until the child is removed from ClippingBorder
    ''' </Remarks>
    
    Public Class ClippingBorder
    Inherits Border
    Protected Overrides Sub OnRender(ByVal dc As DrawingContext)
        OnApplyChildClip()
        MyBase.OnRender(dc)
    End Sub
    
    Public Overrides Property Child() As UIElement
        Get
            Return MyBase.Child
        End Get
        Set(ByVal value As UIElement)
            If Me.Child IsNot value Then
                If Me.Child IsNot Nothing Then
                    ' Restore original clipping
                    Me.Child.SetValue(UIElement.ClipProperty, _oldClip)
                End If
    
                If value IsNot Nothing Then
                    _oldClip = value.ReadLocalValue(UIElement.ClipProperty)
                Else
                    ' If we dont set it to null we could leak a Geometry object
                    _oldClip = Nothing
                End If
                MyBase.Child = value
            End If
        End Set
    End Property
    
    Protected Overridable Sub OnApplyChildClip()
        Dim _child As UIElement = Me.Child
        If _child IsNot Nothing Then
            _clipRect.RadiusX = InlineAssignHelper(_clipRect.RadiusY, Math.Max(0.0, Me.CornerRadius.TopLeft - (Me.BorderThickness.Left * 0.5)))
            _clipRect.Rect = New Rect(_child.RenderSize)
            _child.Clip = _clipRect
        End If
    End Sub
    
    Private _clipRect As New RectangleGeometry()
    Private _oldClip As Object
    Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
        target = value
        Return value
    End Function
    
    End Class
    

    如果你更喜欢 C#,可以使用 http://www.developerfusion.com/tools/convert/vb-to-csharp/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      • 2015-01-15
      • 2023-03-30
      • 2017-11-17
      • 1970-01-01
      相关资源
      最近更新 更多