Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7591559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:40:53+00:00 2026-05-30T20:40:53+00:00

I am trying to create a custom shape class, that is basically a hexagon,

  • 0

I am trying to create a custom shape class, that is basically a hexagon, but with few extra properties and methods.

I tried inheriting it from the Polygon class, but it would not allow it, as the Polygon class is sealed, so I inherited it from the abstract Shape class, but don’t know what to do next. So far my code looks like this

public class Cell : Shape
{
    private Polygon poly;
    private Point[] points = new Point[6];
    public PointCollection Points { get; set; }
    public double Radius { get; set; }
    public Point Center { get; set; }
    public Cell()
    {
        points[0] = new Point(Center.X - 1 / 2 * Radius, Center.Y - Radius * 0.866);
        points[1] = new Point(Center.X + 1 / 2 * Radius, Center.Y - Radius * 0.866);
        points[2] = new Point(Center.X + Radius, Center.Y);
        points[3] = new Point(Center.X + 1 / 2 * Radius, Center.Y + Radius * 0.866);
        points[4] = new Point(Center.X - 1 / 2 * Radius, Center.Y + Radius * 0.866);
        points[5] = new Point(Center.X - Radius, Center.Y);

        Points = new PointCollection();
        foreach (Point p in points)
            Points.Add(p);
        poly = new Polygon();
        poly.Points = this.Points;
    }
}

Now, I want to be able to declare it in XAML as such:

<local:Cell Center="20,20" Radius="10" Stroke="Blue" Fill="White"/>

And I also want it to be visible in the constructor.

What else do I need to add to my class to be able to do that? Is there a certain interface I need to implement or method that I need to override?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:40:54+00:00Added an answer on May 30, 2026 at 8:40 pm

    I did something similar to produce a semicircle shape. You need to inherit from Shape and override DefiningGeometry and MeasureOverride. In DefiningGeometry you will do the actual drawing and return the geometry.

    For you properties to be visible in XAML you just need to add properties to your class. Plain .NET properties will show up, but you’ll probably want to add dependency properties and set the AffectsRender FrameworkPropertyMetadataOptions so that it will force a redraw when you change the properties.

    Here’s what my class looked like:

    public class SemiCircle : Shape
    {
        /// <summary>
        /// Gets or set the alignment of the semicircle. I.e. where should the flat part point.
        /// </summary>
        public SemiCircleAlignment Alignment
        {
            get { return (SemiCircleAlignment)GetValue(AlignmentProperty); }
            set { SetValue(AlignmentProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for alignment.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AlignmentProperty =
            DependencyProperty.Register("Alignment", typeof(SemiCircleAlignment), typeof(SemiCircle), 
            new FrameworkPropertyMetadata(SemiCircleAlignment.Top,FrameworkPropertyMetadataOptions.AffectsRender));
    
        protected override System.Windows.Media.Geometry DefiningGeometry
        {
            get 
            {
                StreamGeometry geometry = new StreamGeometry();
                using (StreamGeometryContext context = geometry.Open())
                {
                    DrawSemiCircle(context);
                }
                geometry.Freeze();
                return geometry;
            }
        }
    
        protected override Size MeasureOverride(Size constraint)
        {
            if (constraint.Height == double.PositiveInfinity || constraint.Width == double.PositiveInfinity)
            {
                if (double.IsNaN(Width) || double.IsNaN(Height))
                {
                    return new Size(0, 0);
                }
                return new Size(Width, Height);
            }
            return constraint;
        }
    
        private void DrawSemiCircle(StreamGeometryContext context)
        {
            double tOff = StrokeThickness / 2.0;                // an offset to account for stroke thickness
            Point startPt = new Point(tOff, tOff);                                   // upper left corner
            if (Alignment == SemiCircleAlignment.Bottom || Alignment == SemiCircleAlignment.Right)
            {
                startPt = new Point(ActualWidth - tOff, ActualHeight - tOff);        // or lower right corner
            }
            Point endPt = new Point(ActualWidth - tOff,tOff);                         // upper right corner
            if (Alignment == SemiCircleAlignment.Left || Alignment == SemiCircleAlignment.Bottom)
            {
                endPt = new Point(tOff, ActualHeight - tOff);                         // or lower left corner
            }
            Size s = new Size(Math.Max(0.0,(ActualWidth / 2) - tOff), 
                Math.Max(0,ActualHeight - StrokeThickness));    // half width is radius
            SweepDirection sweep = SweepDirection.Counterclockwise;    
            if (Alignment == SemiCircleAlignment.Left || Alignment == SemiCircleAlignment.Right)
            {
                s = new Size(Math.Max(0,ActualWidth - StrokeThickness),
                    Math.Max(0.0,(ActualHeight / 2) - tOff));     // or half height is radius
                sweep = SweepDirection.Clockwise;
            }
    
            context.BeginFigure(startPt, true, true);
            context.ArcTo(endPt, s, 0, false, sweep, true, false);
        }
    }
    
    public enum SemiCircleAlignment { Left, Top, Right, Bottom };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a button that has a custom shape (hexagon), but
I'm trying to create a custom JSP tag that would take an array object
I am trying to create a custom accordion for my page to that display
I'm trying to create a custom UIButton that should look like a UIButtonTypeRoundedRect. In
I am trying to create a custom property in a extended control, so that
Trying to create a custom component that gets it's layout from an XML file
I'm trying to create a custom ComboBox that behaves like the one in here:
I'm trying to create a custom control that can be shared by both Silverlight
I'm trying to create custom view that draws image downloaded from Url. The code
I am trying to create custom iterator element. The class itself is below. public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.