lang="xml">< Code : using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; namespace WpfApplication4 { public class PanelClass : Panel { double mHeight = 0.0; double mWidth = 0.0; protected override Size MeasureOverride(Size availableSize) { foreach (UIElement child in this.Children) { child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); if (child.DesiredSize.Width > mWidth) { mWidth = child.DesiredSize.Width; } mHeight += child.DesiredSize.Height; } if (double.IsPositiveInfinity(availableSize.Width)) { return new Size(mWidth, mHeight); } else { return new Size(availableSize.Width, mHeight); } } protected override Size ArrangeOverride(Size finalSize) { double x = 0.0; double y = 0.0; double fw = finalSize.Width; double TotRightWidth = 0.0; double TotWidth = 0.0; HorizontalAlignment horizontalAlignment = HorizontalAlignment; foreach (UIElement child in this.Children) { if (child is TextBox) { horizontalAlignment = ((TextBox)child).HorizontalAlignment; //Gets horizontal alignment TotWidth += child.DesiredSize.Width; } if (child is TextBlock) { horizontalAlignment = ((TextBlock)child).HorizontalAlignment; //Gets horizontal alignment TotWidth += child.DesiredSize.Width; } if (horizontalAlignment == HorizontalAlignment.Right) { TotRightWidth += child.DesiredSize.Width; } } double DiffWidth = (TotWidth - finalSize.Width) / Children.Count; fw = finalSize.Width - TotRightWidth; foreach (UIElement child in Children) { if (child is TextBox) { horizontalAlignment = ((TextBox)child).HorizontalAlignment; //Gets horizontal alignment //VerticalAlignment verticalAlignment = ((TextBox)child).VerticalAlignment; //Gets vertical alignment } if (child is TextBlock) { horizontalAlignment = ((TextBlock)child).HorizontalAlignment; //Gets horizontal alignment //VerticalAlignment verticalAlignment = ((TextBlock)child).VerticalAlignment; //Gets vertical alignment } if (horizontalAlignment == HorizontalAlignment.Right) { Rect rec = new Rect(new Point(fw , y), child.DesiredSize); child.Arrange(rec); fw += child.DesiredSize.Width; } else { Rect rec = new Rect(new Point(x, y), child.DesiredSize); child.Arrange(rec); x += child.DesiredSize.Width; } } return finalSize; }
< WPF Code: <window x:class=""WpfApplication4.MainWindow"<br" mode="hold" xmlns:x="#unknown" /> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication4" Title="MainWindow" Height="350" Width="500"> <Grid > <local:PanelClass> <TextBox Height="23" HorizontalAlignment="Left" Margin="0" Name="textBox1" VerticalAlignment="Top" Text="Left1" Width="120" /> <!--<TextBlock Height="23" Name="textBlock1" Margin="0" Text="TextBlockLeft" />--> <TextBox Height="23" HorizontalAlignment="Left" Margin="0" Name="textBox2" VerticalAlignment="Top" Text="Left2" Width="120" /> <TextBox Height="23" HorizontalAlignment="Right" Margin="0" Name="textBox3" VerticalAlignment="Top" Text="Right1" Width="120" /> <TextBox Height="23" HorizontalAlignment="Right" Margin="0" Name="textBox4" VerticalAlignment="Top" Text="Right2" Width="120" /> <TextBox Height="23" HorizontalAlignment="Right" Margin="0" Name="textBox5" VerticalAlignment="Top" Text="Right3" Width="120" /> </local:PanelClass> </Grid> </Window>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)