Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带圆角的WPF无边界应用程序_Wpf_Xaml_Border - Fatal编程技术网

带圆角的WPF无边界应用程序

带圆角的WPF无边界应用程序,wpf,xaml,border,Wpf,Xaml,Border,我已经创建了一个WPF应用程序,它是无边界的,并且有一个我想要作为背景的图像。我在绘画中创建了我的图像,因此我没有使角落透明的选项。我想让应用程序具有圆角,但我无法使用XAML获得预期的结果。我尝试添加边框以获得所需的效果,但当我运行应用程序时,图像仍在边框前面。这是我的密码。我做错了什么 <Border BorderBrush="Black" CornerRadius="15,15,15,15" BorderThickness="1,1,1,1" Width="242" Height="

我已经创建了一个WPF应用程序,它是无边界的,并且有一个我想要作为背景的图像。我在绘画中创建了我的图像,因此我没有使角落透明的选项。我想让应用程序具有圆角,但我无法使用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>


我认为您需要将cliptobunds=“True”添加到图像中。

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

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>

这里有一个特殊的边框,ClipToBounds可以完美地工作。如果你把Erez的答案中的“Border”改为“clippingorder”,那么它应该会起作用

''' <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
“”
''作为一种副作用,剪辑顺序将超越任何数据绑定或动画
''其childs UIElement.Clip属性,直到从ClippingOrder中删除该子项
''' 
公共类剪报命令
继承边界
受保护的覆盖子OnRender(ByVal dc作为DrawingContext)
OnApplyChildClip()
MyBase.OnRender(dc)
端接头
Public将属性Child()重写为UIElement
得到
返回MyBase.Child
结束
设置(作为UIElement的ByVal值)
如果我的孩子没有价值的话
如果我,孩子不是什么都不是
'恢复原始剪辑
Me.Child.SetValue(UIElement.ClipProperty,\u oldClip)
如果结束
如果值不是零,那么
_oldClip=value.ReadLocalValue(UIElement.ClipProperty)
其他的
'如果我们不将其设置为null,可能会泄漏几何体对象
_oldClip=无
如果结束
MyBase.Child=value
如果结束
端集
端属性
ApplyChildClip()上受保护的可重写子对象
Dim\u child作为UIElement=Me.child
如果你的孩子不是什么都不是
_clipRect.RadiusX=InlineAssignHelper(_clipRect.RadiusY,Math.Max(0.0,Me.CornerRadius.TopLeft-(Me.BorderThickness.Left*0.5)))
_clipRect.Rect=新的Rect(_child.renderize)
_child.Clip=\u clipRect
如果结束
端接头
Private _clipRect作为新的矩形几何体()
Private _oldClip作为对象
InlineAssignHelper(Of T)中的私有共享函数(ByRef目标为T,ByVal值为T)为T
目标=价值
返回值
端函数
末级

如果你更喜欢C,你可以用它。

我不知道为什么我想要一个椭圆。。。。我有一个矩形图像,我想要圆角。我不想有elipse.poco我已经更新了我的答案,是有边框的,不是有elipse的我试过了,但是边框还是被放在了图片后面。。。我能找到的唯一解决办法就是上面列出的那个。我最终解决这个问题的办法是在油漆车间编辑照片,使角落透明。。一旦完成,我删除了边框,上面的代码就可以工作了。谢谢erez和andrew的时间和帮助,但是我无法从你的回答中得到预期的结果。我已经更新了我的答案。试试看。