Mega Code Archive

 
Categories / C# Tutorial / WPF
 

Override the default layout behavior of the Panel element and create custom layout elements that are derived from Panel

using System; using System.Windows; using System.Windows.Controls; using System.Windows.Shapes; using System.Windows.Media; using System.Threading;         public class PlotPanel : Panel         {             public PlotPanel(): base()             {             }             protected override Size MeasureOverride(Size availableSize)             {                 Size childSize = availableSize;                 foreach (UIElement child in InternalChildren)                 {                     child.Measure(childSize);                 }                 return availableSize;             }             protected override Size ArrangeOverride(Size finalSize)             {                 foreach (UIElement child in InternalChildren)                 {                     double x = 50;                     double y = 50;                     child.Arrange(new Rect(new Point(x, y), child.DesiredSize));                 }                 return finalSize;              }         }   public class app : System.Windows.Application   {         PlotPanel plot1 = new PlotPanel();         Window mainWindow = new System.Windows.Window();         Rectangle rect1 = new Rectangle();         protected override void OnStartup (StartupEventArgs e)     {         base.OnStartup (e);             plot1.Background = Brushes.White;             rect1.Fill = Brushes.CornflowerBlue;             rect1.Width = 200;             rect1.Height = 200;             mainWindow.Content = plot1;             plot1.Children.Add(rect1);             mainWindow.Title = "Custom Panel Sample";             mainWindow.Show();     }     [System.STAThread()]     private static void Main()     {       app app = new app();       app.Run();     }   }