Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to set ItemSource of a custom panel

$
0
0

Dear all,

I have a class named Circle panel which Inherit from Panel and define as follow:

 class CirclePanel:Panel
    {
        public double OuterRadius
        {
            get { return (double)GetValue(OuterRadiusProperty); }
            set { SetValue(OuterRadiusProperty, value); }
        }

        public static readonly DependencyProperty OuterRadiusProperty =
                               DependencyProperty.Register("OuterRadius", typeof(double), typeof(CirclePanel), new UIPropertyMetadata(0.0,
                                (o, e) =>
                                {
                                    (o as CirclePanel).Width = (double)e.NewValue * 2;
                                    (o as CirclePanel).Height = (double)e.NewValue * 2;
                                }));

        public double InnerRadius
        {
            get { return (double)GetValue(InnerRadiusProperty); }
            set { SetValue(InnerRadiusProperty, value); }
        }

        public static readonly DependencyProperty InnerRadiusProperty =
                                 DependencyProperty.Register("InnerRadius", typeof(double), typeof(CirclePanel), new UIPropertyMetadata(0.0));

        public double AngleForLayout
        {
            get { return (double)GetValue(AngleForLayoutProperty); }
            set { SetValue(AngleForLayoutProperty, value); }
        }

        public static readonly DependencyProperty AngleForLayoutProperty =
                                 DependencyProperty.Register("AngleForLayout", typeof(double), typeof(CirclePanel), new UIPropertyMetadata(0.0));



        protected override System.Windows.Size MeasureOverride(System.Windows.Size availableSize)
        {
            //We scan for each chidl and get the  size of each
            foreach (UIElement child in Children)
            {
                child.Measure(availableSize);
            }
            return new Size(2 * OuterRadius, 2 * OuterRadius);

        }

        protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize)
        {
            Point currentPosition = new Point(OuterRadius, (OuterRadius - InnerRadius) / 2);
            int childCount = Children.Count;
            //double perAngle = (2* Math.PI) / childCount;
            double _incrementalAngularSpace = (AngleForLayout / Children.Count) * (Math.PI / 180);

            double OffsetX = 0.0, OffsetY = 0.0;
            for (int i = 0; i < childCount; i++)
            {
                UIElement child = Children[i];
                double angle = (i + 1) * _incrementalAngularSpace;
                OffsetX = Math.Sin(angle) * (OuterRadius + InnerRadius) / 2;
                OffsetY = (1 - Math.Cos(angle)) * (OuterRadius + InnerRadius) / 2;
                Rect childRect = new Rect(
                 new Point(currentPosition.X - child.DesiredSize.Width / 2, currentPosition.Y - child.DesiredSize.Height / 2),
                 new Point(currentPosition.X + child.DesiredSize.Width / 2, currentPosition.Y + child.DesiredSize.Height / 2));
                child.Arrange(childRect);
                currentPosition.X = OffsetX + OuterRadius;
                currentPosition.Y = OffsetY + (OuterRadius - InnerRadius) / 2;
            }
            return new Size(2 * OuterRadius, 2 * OuterRadius);

        }
    }

When I use this class in my WPF control as follow it works perfectly well :

<circlepanels:CirclePanel DataContext="{Binding MainMenuViewModel,UpdateSourceTrigger=PropertyChanged}" 
                                      InnerRadius="100" 
                                      OuterRadius="200" 
                                      AngleForLayout="270"
                                      HorizontalAlignment="Center" VerticalAlignment="Center" ><controls:ButtonCommandControl /><controls:ButtonCommandControl /><controls:ButtonCommandControl /></circlepanels:CirclePanel>

From teh code above my button gets perfecly render on a cirlce radius. What I am trying to do now is to populate the Childs through an ItemSource binding. I could not find or access to an ItemSource property in the panel.

How can I set an ItemSource for my CirclePanel?

regards


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>