Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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
C#WPF使用多边形剪裁控件_C#_Wpf_Geometry - Fatal编程技术网

C#WPF使用多边形剪裁控件

C#WPF使用多边形剪裁控件,c#,wpf,geometry,C#,Wpf,Geometry,我正在尝试制作一个自定义ContentControl,它的形状为圆角多边形。由于某种原因,当我在控件上设置Clip属性时,什么也没有显示。感谢您的帮助 PageHost.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Media; using System.Windows.Shap

我正在尝试制作一个自定义ContentControl,它的形状为圆角多边形。由于某种原因,当我在控件上设置Clip属性时,什么也没有显示。感谢您的帮助

PageHost.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;

namespace DtcInvoicer.Controls
{
    public class PageHost:UserControl
    {
        #region public ImageSource Icon;
        public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(PageHost));
        public ImageSource Icon
        {
            get { return GetValue(IconProperty) as ImageSource; }
            set { SetValue(IconProperty, value); }
        }
        #endregion

        #region public string Title;
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(PageHost));
        public string Title
        {
            get { return GetValue(TitleProperty).ToString(); }
            set { SetValue(TitleProperty, value); }
        }
        #endregion

        #region public double Radius;
        public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register("Radius", typeof(double), typeof(PageHost));
        public double Radius
        {
            get { return (double)GetValue(RadiusProperty); }
            set 
            { 
                SetValue(RadiusProperty, value); 
                DoClip(); 
            }
        }
        #endregion

        public PageHost()
        {
            Loaded += new RoutedEventHandler(PageHost_Loaded);
            SizeChanged += new SizeChangedEventHandler(PageHost_SizeChanged);
        }

        #region Event Handlers
        private void PageHost_Loaded(object sender, RoutedEventArgs e)
        {
            DoClip();
        }

        private void PageHost_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            DoClip();
        }
        #endregion

        private void DoClip()
        {
            Polygon p = new Polygon()
            { 
                Points = new PointCollection()
                {
                    new Point(0, 0),
                    new Point(ActualWidth - 30, 0),
                    new Point(ActualWidth, 30),
                    new Point(ActualWidth, ActualHeight),
                    new Point(0, ActualHeight)
                }
            };

            Geometry g1 = new RectangleGeometry(new Rect(0, 0, ActualWidth, ActualHeight), Radius, Radius);
            Geometry g2 = p.RenderedGeometry;

            // Clip = g1; this works, the control shows up with the rounded corners
            // Clip = g2; this does not work, nothing shows up

            // this is what I want to do, I want to combine the two geometries
            // but this does not work either
            Clip = new CombinedGeometry(GeometryCombineMode.Intersect, g1, g2);
        }
    }
}
主页.xaml

<control:PageHost x:Class="DtcInvoicer.Pages.HomePage"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:control="clr-namespace:DtcInvoicer.Controls"
         Width="500" Height="250" Radius="20" Background="Aqua">
</control:PageHost>

在这种情况下,将剪辑设置为RenderedGeometry会失败,因为RenderedGeometry尚未实际渲染,因此不可用。对于常规几何图形,请在DoClip中使用:

Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(delegate
{
    Clip = new CombinedGeometry(GeometryCombineMode.Intersect, g1, g2); 
}));
使用RenderedGeometry,需要将其添加到视觉树的某个位置,然后在设置剪辑区域之前使用其加载的事件,这将很困难。尝试使用具有相同点的常规几何体而不是渲染几何体,例如。下面是我使用PathGeometry绘制三角形的示例:

double leftPoint = cellRect.Right - 12;
if (leftPoint < cellRect.Left)
    leftPoint = cellRect.Left;
double topPoint = cellRect.Top + (cellRect.Height - 4.0) / 2;
if (topPoint < cellRect.Top)
    topPoint = cellRect.Top;
double rightPoint = leftPoint + 7;
if (rightPoint > cellRect.Right)
    rightPoint = cellRect.Right;
double bottomPoint = topPoint + 4;
if (bottomPoint > cellRect.Bottom)
    bottomPoint = cellRect.Bottom;
double middlePoint = leftPoint + 3;
if (middlePoint > cellRect.Right)
    middlePoint = cellRect.Right;

PathFigure figure = new PathFigure();
figure.StartPoint = new Point(middlePoint, bottomPoint);
PathFigureCollection figCollection = new PathFigureCollection();
figCollection.Add(figure);

PathSegmentCollection segCollection = new PathSegmentCollection();
LineSegment topSeg = new LineSegment();
topSeg.Point = new Point(rightPoint, topPoint);
segCollection.Add(topSeg);
LineSegment midRightSeg = new LineSegment();
midRightSeg.Point = new Point(leftPoint, topPoint);
segCollection.Add(midRightSeg);
LineSegment midLeftSeg = new LineSegment();
midLeftSeg.Point = new Point(middlePoint+1, bottomPoint);
segCollection.Add(midLeftSeg);
figure.Segments = segCollection;

PathGeometry geo = new PathGeometry();
geo.Figures = figCollection;
double leftPoint=cellRect.Right-12;
if(leftPointcellRect.Right)
rightPoint=cellRect.Right;
双底点=顶点+4;
if(底部点>单元格垂直底部)
bottomPoint=cellRect.Bottom;
双中点=左中点+3;
如果(中点>单元格右端)
中点=单元格右端;
PathFigure=新的PathFigure();
figure.StartPoint=新点(中点、低点);
PathFigureCollection figCollection=新的PathFigureCollection();
figCollection.Add(图);
PathSegmentCollection segCollection=新的PathSegmentCollection();
LineSegment topSeg=新线段();
topSeg.Point=新点(右点、顶点);
segCollection.Add(topSeg);
LineSegment midRightSeg=新线段();
middrightseg.Point=新点(leftPoint,topPoint);
segCollection.Add(midRightSeg);
LineSegment midLeftSeg=新线段();
middleftseg.Point=新点(中点+1,底点);
segCollection.Add(midLeftSeg);
图.分段=分段集合;
PathGeometry geo=新的PathGeometry();
地理数字=figCollection;