WPF listview rounded corner

Source: Internet
Author: User

I haven't found it online for a long time.

Three methods are implemented by yourself.

1. Use clip attributes

2. Adorner with decorator

3. Modify the Template

 

1. Clip is to draw a rounded rectangle.

However, this method is also called when the listview size changes.

The Code is as follows:

 1 using System; 2 using System.Windows; 3 using System.Windows.Media; 4  5     class ClipRadiusView 6     { 7         private static double DisX = 20; 8         private static double DisY = 20; 9         static Size XY=new Size(DisX,DisY);10         11         private static bool isLarge = false;12         private static bool isStroke = true;13         private static double angle = 150;14 15         private static int offx = 3;16         private static int offy = 2;17 18         static Point StartPoint = new Point(offx, DisY);19         static Point P1 = new Point(DisX, offy);20         21 22         public static void InitClip(FrameworkElement el)23         {24             double w = el.ActualWidth;25             double h = el.ActualHeight;26             if(w<DisX*2||h<DisY*2)27                 return;28 29             30             PathFigure myFigure=new PathFigure();31 32            33             34             myFigure.StartPoint = StartPoint;35 36             ArcSegment a1=new ArcSegment(P1, XY, angle,isLarge,SweepDirection.Clockwise, isStroke);37             myFigure.Segments.Add(a1);38 39             Point p2 = new Point(w - DisX, offy);40             LineSegment l1 = new LineSegment(p2, true);41             myFigure.Segments.Add(l1);42 43             Point p3 = new Point(w, DisY);44             ArcSegment a2 = new ArcSegment(p3, XY, angle, isLarge, SweepDirection.Clockwise, isStroke);45             myFigure.Segments.Add(a2);46 47             Point p4 = new Point(w, h - DisY);48             LineSegment l2 = new LineSegment(p4, true);49             myFigure.Segments.Add(l2);50 51             Point p5 = new Point(w - DisX, h);52             ArcSegment a3 = new ArcSegment(p5, XY, angle, isLarge, SweepDirection.Clockwise, isStroke);53             myFigure.Segments.Add(a3);54 55             Point p6 = new Point(DisX, h);56             LineSegment l3 = new LineSegment(p6, true);57             myFigure.Segments.Add(l3);58 59             Point p7 = new Point(offx, h - DisY);60             ArcSegment a4 = new ArcSegment(p7, XY, angle, isLarge, SweepDirection.Clockwise, isStroke);61             myFigure.Segments.Add(a4);62 63             //Point p8 = new Point(offx, DisY);64             //LineSegment l4 = new LineSegment(p8, true);65             //myFigure.Segments.Add(l4);66 67             myFigure.IsClosed = true;68             PathGeometry pg = new PathGeometry();69             pg.Figures.Add(myFigure);70             el.ClipToBounds = true;71             el.Clip = pg;72         }73     }

2. decorator adorner

That is, draw a rounded corner with the same background color as the four corners.

This will change with the size change

Using system. windows; using system. windows. documents; using system. windows. media; Class radiusadorner: adorner {private brush _ backbrush = brushes. white; private pen renderpen; Public radiusadorner (uielement El, brush pen): Base (EL) {If (pen! = NULL) _ backbrush = pen; renderpen = new pen (_ backbrush, 0);} Private Static double disx = 20; Private Static double DISY = 20; size xy = new size (disx, DISY); Private bool islarge = false; private bool isstroke = true; private double angle = 90; private double pensize = 2; private point _ start; private pathgeometry _ PG; private pathfigure _ pF; private point _ start1; private point _ P1; private Po Int _ P2; private linesegment _ line; private arcsegment _ arc; private rect _ adornedelementrect; protected override void onrender (drawingcontext) {_ adornedelementrect = new rect (this. adornedelement. rendersize); double W = _ adornedelementrect. width; double H = _ adornedelementrect. height; _ start = new point (_ adornedelementrect. x, _ adornedelementrect. y); _ Pg = new pathgeometry (); _ pF = New pathfigure (); // _ start1 = _ start; _ pF = new pathfigure (); _ pF. startpoint = _ start1; _ p1 = new point (_ start1.x + disx, _ start1.y); _ line = new linesegment (_ P1, true); _ pF. segments. add (_ line); _ P2 = new point (_ start1.x, _ start1.y + DISY); _ arc = new arcsegment (_ P2, XY, angle, islarge, sweepdirection. counterclockwise, isstroke); _ pF. segments. add (_ arc); _ pF. isclosed = true; _ PG. figures. add (_ pF ); // Draw a line on the top // _ start1 = new point (_ start. x, _ start. Y + 2); // _ pF = new pathfigure (); // _ pF. startpoint = _ start1; // _ p1 = new point (_ start1.x + W, _ start1.y); // _ line = new linesegment (_ P1, true ); // _ pF. segments. add (_ line); // _ PG. figures. add (_ pF); // _ start1 = new point (_ start. X + W, _ start. y); _ pF = new pathfigure (); _ pF. startpoint = _ start1; _ p1 = new point (_ start1.x-disx, _ start1.y); _ line = new Linesegment (_ P1, true); _ pF. segments. add (_ line); _ P2 = new point (_ start1.x, _ start1.y + DISY); _ arc = new arcsegment (_ P2, XY, angle, islarge, sweepdirection. clockwise, isstroke); _ pF. segments. add (_ arc); _ pF. isclosed = true; _ PG. figures. add (_ pF); // lower right corner _ start1 = new point (_ start. X + W, _ start. Y + H); _ pF = new pathfigure (); _ pF. startpoint = _ start1; _ p1 = new point (_ start1.x-disx, _ start1.y); _ Li Ne = new linesegment (_ P1, true); _ pF. segments. add (_ line); _ P2 = new point (_ start1.x, _ start1.y-DISY); _ arc = new arcsegment (_ P2, XY, angle, islarge, sweepdirection. counterclockwise, isstroke); _ pF. segments. add (_ arc); _ pF. isclosed = true; _ PG. figures. add (_ pF); // _ start1 = new point (_ start. x, _ start. Y + H); _ pF = new pathfigure (); _ pF. startpoint = _ start1; _ p1 = new point (_ start1.x + disx, _ St Art1.y); _ line = new linesegment (_ P1, true); _ pF. segments. add (_ line); _ P2 = new point (_ start1.x, _ start1.y-DISY); _ arc = new arcsegment (_ P2, XY, angle, islarge, sweepdirection. clockwise, isstroke); _ pF. segments. add (_ arc); _ pF. isclosed = true; _ PG. figures. add (_ pF); drawingcontext. drawgeometry (renderpen. brush, renderpen, _ PG);} static brush defaulbrushe = new solidcolorbrush (New Color {A = 255, r = 137, G = 181, B = 233}); public static void addradiusadorner (frameworkelement El) {radiusadorner Ra = new radiusadorner (El, defaulbrushe); adornerlayer Al = adornerlayer. getadornerlayer (EL); If (Al! = NULL) {Al. Add (RA );}}}

3. Template

Is to modify the default template of listview, refer to the change in the msdn listview Style

However, the top part of the gridview cannot be rounded.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.