Click here to Skip to main content
15,910,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir,

I am new to WPF, I try to developing a simple panel. In the panel arranging the textbox and textblock to Left and Right, i cannot reduce the Textbox width according to the form size. help me to improve knowledge and codes are following
thanks

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;
 }


XML
<
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>
Posted
Updated 2-Sep-11 20:03pm
v2
Comments
Simon Bang Terkildsen 3-Sep-11 10:27am    
What exactly are you trying to achieve? I'm sure you don't need to make your own panel.
gpasupathi 4-Sep-11 23:57pm    
Dear sir, I am trying to achieve, how panels are working and control the panel commend from XMAL codes and Dynamically commend

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900