Click here to Skip to main content
15,917,795 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, Right and Centre.

How can i get the Textbox HorizontalAlignment is Left or Right and Verticalaignment is top or bottom from the XAML code

my code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace MySimplePanel
{
    public class MySimplePanelClass : Panel
    {
        protected override Size MeasureOverride(Size availableSize)
        {
            Size maxSize = new Size();
            foreach (UIElement child in this.Children)
            {
                child.Measure(availableSize);
                maxSize.Height = Math.Max(child.DesiredSize.Height, maxSize.Height);
                maxSize.Width = Math.Max(child.DesiredSize.Width, maxSize.Width);

            }
            return maxSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            foreach (UIElement child in InternalChildren)
            {
                child.Arrange(new Rect(finalSize));
                //if (HorizontalAlignment == HorizontalAlignment.Right)
                //{
                //}
            }
            return finalSize;
        }
}


XAML code :
HTML
<window x:class="MySimplePanel.MainWindow" 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:MySimplePanel"
        Title="MainWindow" Height="350" Width="525">
    <grid>
        <local:mysimplepanelclass grid.columnspan="5" horizontalalignment="Stretch" xmlns:local="#unknown">
            <textbox height="23" horizontalalignment="Left" margin="0,10,0,0" name="textBox1" verticalalignment="Top" width="120" />
            <textbox height="23" horizontalalignment="Left" margin="100,10,0,0" name="textBox2" verticalalignment="Top" width="120" textalignment="Center" />
            <textbox height="23" horizontalalignment="Right" margin="200,10,0,0" name="textBox3" verticalalignment="Top" width="120" />
            <textbox height="23" horizontalalignment="Right" margin="360,10,0,0" name="textBox4" verticalalignment="Top" width="120" />
        </local:mysimplepanelclass>

    </grid>
</window>


Thanks all
Posted
Updated 1-Sep-11 22:49pm
v2
Comments
Abhinav S 2-Sep-11 4:49am    
Code tags added.

Try the following in your code -
C#
protected override Size ArrangeOverride(Size finalSize)
{
    foreach (UIElement child in InternalChildren)
    {
        child.Arrange(new Rect(finalSize));
        if (child is TextBox)
        {
            HorizontalAlignment horizontalAlignment = ((TextBox)child).HorizontalAlignment; //Gets horizontal alignment
            VerticalAlignment verticalAlignment = ((TextBox)child).VerticalAlignment; //Gets vertical alignment
        }
    }
    return finalSize;
}
 
Share this answer
 
v2
Comments
gpasupathi 2-Sep-11 5:01am    
many thanks
gpasupathi 2-Sep-11 5:05am    
dear sir, any sample for alignment with Dependency Property
Abhinav S 2-Sep-11 5:30am    
Alignment is a Dependecy Property. When you set it in the xaml, you are setting the dependency property itself.
Simon Bang Terkildsen 2-Sep-11 5:52am    
+5 . However I would love to know why the OP has the need to implement his own Panel.
Abhinav S 2-Sep-11 6:11am    
Thanks for the 5.
That is a good question for the OP.
can change width or margin or thickness of the TextBox
thanks

Code :
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;
}
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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