public void UpdateStackPanel()
{
if(( _myLayoutRoot != null ) && (_myGrids.Count > 0) )
{
double dbWidth = _myLayoutRoot.ActualWidth;
int nLineCounter = 0;
double dbLineLength = (double)0.0;
int nWordCounter = 0;
int nWordLineCounter = 0;
// ** walk through all my words
while( nWordCounter < _myTextBlocks.Count )
{
// ** get the TextBlock
TextBlock txtText = _myTextBlocks[ nWordCounter ];
// ** get the Grid for the Line
Grid grGrid = ( nLineCounter < _myGrids.Count ) ? _myGrids[ nLineCounter ] : CreateNewGridAndAppend();
// ** now get the row and col
// ** the first word in the line is always fix placed
if( nWordLineCounter == 0 )
{
// ** Set the Parent
SetNewParent( txtText, grGrid );
// ** set col, row and width
grGrid.ColumnDefinitions[ 0 ].Width = new GridLength( txtText.ActualWidth, GridUnitType.Pixel );
Grid.SetColumn( txtText, 0 );
Grid.SetRow( txtText, 0 );
// ** increase the counters
nWordCounter++;
nWordLineCounter++;
dbLineLength = txtText.ActualWidth;
}
else
{
// ** calculate the position, where the TextBlock has to be entered
int nSpaceColIndex = ( nWordLineCounter * 2 ) - 1;
int nWordColIndex = ( nWordLineCounter * 2 );
// ** it is not the first word in the line, so check if it fits into this line
if( ( this.ActualWidth - dbLineLength - this.MinSpaceWidth ) > txtText.ActualWidth )
{
ColumnDefinition colDefSpace = null, colDefWord = null;
// ** do I have the needed Col's
if( grGrid.ColumnDefinitions.Count <= nSpaceColIndex )
{
colDefSpace = new ColumnDefinition();
grGrid.ColumnDefinitions.Add( colDefSpace );
}
else
{
colDefSpace = grGrid.ColumnDefinitions[ nSpaceColIndex ];
}
// ** set the Width to Star, so the space get's calculated automatically
colDefSpace.Width = new GridLength( (double)1.0, GridUnitType.Star );
colDefSpace.MinWidth = this.MinSpaceWidth;
// ** do I have the needed Col's
if( grGrid.ColumnDefinitions.Count <= nWordColIndex )
{
colDefWord = new ColumnDefinition();
grGrid.ColumnDefinitions.Add( colDefWord );
}
else
{
colDefWord = grGrid.ColumnDefinitions[ nWordColIndex ];
}
// ** Set the width of the Word Column
colDefWord.Width = new GridLength( txtText.ActualWidth, GridUnitType.Pixel );
// ** Set the Parent of the TextBlock
SetNewParent( txtText, grGrid );
// ** set the position of the word
Grid.SetColumn( txtText, nWordColIndex );
Grid.SetRow( txtText, 0 );
// ** increase the counters
nWordLineCounter++;
nWordCounter++;
dbLineLength += this.MinSpaceWidth + txtText.ActualWidth;
}
else
{
// ** if there is a new line needed, so cut the old if
// ** there are too much columns and set the horalign of the
// ** last word to right
while( grGrid.ColumnDefinitions.Count > ( nWordColIndex - 1 ) )
{
grGrid.ColumnDefinitions.RemoveAt( nWordColIndex - 1 );
}
// ** set the grid to stretch !! If this was a last line sometimes the
// ** stretch were changed to left
grGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
// ** the first word in this line is left oriented, the last one right
( (TextBlock)( grGrid.Children[ 0 ] ) ).HorizontalAlignment = HorizontalAlignment.Left;
( (TextBlock)( grGrid.Children[ grGrid.Children.Count-1 ] ) ).HorizontalAlignment = HorizontalAlignment.Right;
// ** prepare for next line
nWordLineCounter = 0;
nLineCounter++;
dbLineLength = (double)0.0;
}
}
}
// ** the last grid ist leftoriented
_myGrids[ nLineCounter ].HorizontalAlignment = HorizontalAlignment.Left;
// ** remove unused grids
while( _myLayoutRoot.Children.Count > ( nLineCounter + 1 ) )
{
_myLayoutRoot.Children.RemoveAt( ( nLineCounter + 1 ) );
_myGrids.RemoveAt( ( nLineCounter + 1 ) );
}
}
}