天天看點

WPF 控件自定義背景

<!--控件要設定尺寸的話,設定的尺寸必須比下面的圖形的尺寸要小,不然顯示不開-->
        <Label  Content="直角測試"  Width="90" Height="90" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="359,207,283,62" >
            <Label.Background>
                <VisualBrush >
                    <VisualBrush.Visual>
                        <Path   Stroke="Red" SnapsToDevicePixels="True" StrokeThickness="3" >
                            <Path.Data>
                                <PathGeometry>
                                    <PathFigure StartPoint="0,0">
                                        <!--折線圖-->
                                        <PolyLineSegment  Points="100,0 100,90 55,90 50,100 45,90 0,90 0,0"></PolyLineSegment>
                                    </PathFigure>
                                </PathGeometry>
                            </Path.Data>
                        </Path>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Label.Background>
        </Label>


        <Label  Content="圓角測試" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="16,273,653,20"  >
            <Label.Background>
                <VisualBrush Stretch="Fill">
                    <VisualBrush.Visual>
                        <Path Stroke="Red" StrokeThickness="2" Stretch="UniformToFill">
                            <Path.Data>
                                <PathGeometry>
                                    <!--路徑開始點-->
                                    <PathFigure StartPoint="5,0">
                                        <!--線段-->
                                        <LineSegment Point="95,0"></LineSegment>
                                        <!--弧形-->
                                        <ArcSegment Point="100,5" Size="5,5" SweepDirection="Clockwise"></ArcSegment>
                                        <!--線段-->
                                        <LineSegment Point="100,85"></LineSegment>
                                        <!--弧形-->
                                        <ArcSegment Point="95,90" Size="5,5" SweepDirection="Clockwise"></ArcSegment>
                                        <!--折線-->
                                        <PolyLineSegment Points="55,90 50,100 45,90 5,90"></PolyLineSegment>
                                        <!--弧形-->
                                        <ArcSegment Point="0,85" Size="5,5" SweepDirection="Clockwise"></ArcSegment>
                                        <!--線段-->
                                        <LineSegment Point="0,5"></LineSegment>
                                        <!--弧形-->
                                        <ArcSegment Point="5,0" Size="5,5" SweepDirection="Clockwise"></ArcSegment>
                                    </PathFigure>
                                </PathGeometry>
                            </Path.Data>
                        </Path>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Label.Background>
        </Label>           

效果圖

WPF 控件自定義背景