首页 > 代码库 > wpf ListView圆角

wpf ListView圆角

网上找了 好久 没找到好的

自己实现了3种

1.用clip属性

2。用装饰器Adorner

3.修改模板

 

1。clip 就是画一个圆角矩形

但是这个在listview  size 变化时 也要调用这个方法

代码如下:

 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.装饰器Adorner

就是再四个角落画一个和背景色一样的圆角

这个 会随着尺寸变化而变化

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 Point _p2;        private LineSegment _line;        private ArcSegment _arc;        private Rect _adornedElementRect;        protected override void OnRender(DrawingContext 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);            ////顶部画一条线             //_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);            //右下角            _start1 = new Point(_start.X + w, _start.Y + h);            _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);            //左下角            _start1 = new Point(_start.X, _start.Y + h);            _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);            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 模板

     就是修改ListView的默认Template  参考msdn listview样式里的 改

     但是gridview的上面部分 不能切成圆角