我创建了一个无边界的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>
答案 0 :(得分:1)
窗口中的这些设置将使其透明:
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"
然后只需将Border的背景设置为图像:
<Border BorderBrush="Transparent" BorderThickness="1" CornerRadius="15">
<Border.Background>
<ImageBrush>
<ImageBrush.ImageSource>
<BitmapImage UriSource="/Test;component/Test.png" />
</ImageBrush.ImageSource>
</ImageBrush>
</Border.Background>
</Border>
答案 1 :(得分:0)
我认为您需要将ClipToBounds =“True”添加到图像中。
答案 2 :(得分:0)
这是ClipToBounds完美运作的特殊边框。如果你改变Erez的回答“边框”到“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/。