1: public void UpdateStackPanel()
2: {
3: if(( _myLayoutRoot != null ) && (_myGrids.Count > 0) )
4: {
5: double dbWidth = _myLayoutRoot.ActualWidth;
6: int nLineCounter = 0;
7: double dbLineLength = (double)0.0;
8: int nWordCounter = 0;
9: int nWordLineCounter = 0;
10:
11: // ** walk through all my words
12: while( nWordCounter < _myTextBlocks.Count )
13: {
14: // ** get the TextBlock
15: TextBlock txtText = _myTextBlocks[ nWordCounter ];
16:
17: // ** get the Grid for the Line
18: Grid grGrid = ( nLineCounter < _myGrids.Count ) ? _myGrids[ nLineCounter ] : CreateNewGridAndAppend();
19:
20: // ** now get the row and col
21: // ** the first word in the line is always fix placed
22: if( nWordLineCounter == 0 )
23: {
24: // ** Set the Parent
25: SetNewParent( txtText, grGrid );
26:
27: // ** set col, row and width
28: grGrid.ColumnDefinitions[ 0 ].Width = new GridLength( txtText.ActualWidth, GridUnitType.Pixel );
29: Grid.SetColumn( txtText, 0 );
30: Grid.SetRow( txtText, 0 );
31:
32: // ** increase the counters
33: nWordCounter++;
34: nWordLineCounter++;
35: dbLineLength = txtText.ActualWidth;
36: }
37: else
38: {
39: // ** calculate the position, where the TextBlock has to be entered
40: int nSpaceColIndex = ( nWordLineCounter * 2 ) - 1;
41: int nWordColIndex = ( nWordLineCounter * 2 );
42:
43: // ** it is not the first word in the line, so check if it fits into this line
44: if( ( this.ActualWidth - dbLineLength - this.MinSpaceWidth ) > txtText.ActualWidth )
45: {
46: ColumnDefinition colDefSpace = null, colDefWord = null;
47:
48: // ** do I have the needed Col's
49: if( grGrid.ColumnDefinitions.Count <= nSpaceColIndex )
50: {
51: colDefSpace = new ColumnDefinition();
52: grGrid.ColumnDefinitions.Add( colDefSpace );
53: }
54: else
55: {
56: colDefSpace = grGrid.ColumnDefinitions[ nSpaceColIndex ];
57: }
58:
59: // ** set the Width to Star, so the space get's calculated automatically
60: colDefSpace.Width = new GridLength( (double)1.0, GridUnitType.Star );
61: colDefSpace.MinWidth = this.MinSpaceWidth;
62:
63: // ** do I have the needed Col's
64: if( grGrid.ColumnDefinitions.Count <= nWordColIndex )
65: {
66: colDefWord = new ColumnDefinition();
67: grGrid.ColumnDefinitions.Add( colDefWord );
68: }
69: else
70: {
71: colDefWord = grGrid.ColumnDefinitions[ nWordColIndex ];
72: }
73:
74: // ** Set the width of the Word Column
75: colDefWord.Width = new GridLength( txtText.ActualWidth, GridUnitType.Pixel );
76:
77: // ** Set the Parent of the TextBlock
78: SetNewParent( txtText, grGrid );
79:
80: // ** set the position of the word
81: Grid.SetColumn( txtText, nWordColIndex );
82: Grid.SetRow( txtText, 0 );
83:
84: // ** increase the counters
85: nWordLineCounter++;
86: nWordCounter++;
87: dbLineLength += this.MinSpaceWidth + txtText.ActualWidth;
88: }
89: else
90: {
91: // ** if there is a new line needed, so cut the old if
92: // ** there are too much columns and set the horalign of the
93: // ** last word to right
94: while( grGrid.ColumnDefinitions.Count > ( nWordColIndex - 1 ) )
95: {
96: grGrid.ColumnDefinitions.RemoveAt( nWordColIndex - 1 );
97: }
98:
99: // ** set the grid to stretch !! If this was a last line sometimes the
100: // ** stretch were changed to left
101: grGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
102:
103: // ** the first word in this line is left oriented, the last one right
104: ( (TextBlock)( grGrid.Children[ 0 ] ) ).HorizontalAlignment = HorizontalAlignment.Left;
105: ( (TextBlock)( grGrid.Children[ grGrid.Children.Count-1 ] ) ).HorizontalAlignment = HorizontalAlignment.Right;
106:
107: // ** prepare for next line
108: nWordLineCounter = 0;
109: nLineCounter++;
110: dbLineLength = (double)0.0;
111: }
112: }
113: }
114:
115: // ** the last grid ist leftoriented
116: _myGrids[ nLineCounter ].HorizontalAlignment = HorizontalAlignment.Left;
117:
118: // ** remove unused grids
119: while( _myLayoutRoot.Children.Count > ( nLineCounter + 1 ) )
120: {
121: _myLayoutRoot.Children.RemoveAt( ( nLineCounter + 1 ) );
122: _myGrids.RemoveAt( ( nLineCounter + 1 ) );
123: }
124: }
125: }