Auto-Size and Center Textblock Inside Stackpanel
I'm currently working on dynamically inserting Textblocks into Stackpanels. This has to be done numerous times, and there is no way to know what the size of the Stackpanel will be beforehand.
Currently, I have something like this:
TextBlock tmp = new TextBlock {
Text = curField.FieldName,
Foreground = new SolidColorBrush(Colors.Red),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
tmp.MouseLeftButtonUp += imgFormImage_MouseLeftButtonUp;
curField.assocStackpanel.Children.Add(tmp);
curField.assocStackpanel = c开发者_Python百科urField.assocStackpanel;
SelectedFields.Add(curField);
Right now, the textblock just shows up centered horizontally in the Stackpanel, but not vertically. So I need to fix that. Also, ideally I would like to be able to dynamically determine the Textblock's font size so that it will fill the available space. Right now I think it's just taking the default of (I believe) 10.
I've decided to go with the following:
double xpos = Canvas.GetLeft(curField.assocGrid);
double ypos = Canvas.GetTop(curField.assocGrid);
double width = curField.assocGrid.Width;
double height = curField.assocGrid.Height;
TextBlock tmp = new TextBlock {
Text = curField.FieldName,
Foreground = new SolidColorBrush(Colors.Red),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
FontSize = 30
};
Grid grd = new Grid();
grd.Children.Add(tmp);
Viewbox vb = new Viewbox();
vb.Child = grd;
vb.Width = width;
vb.Height = height;
cvsCenterPane.Children.Add(vb);
Canvas.SetLeft(vb, xpos);
Canvas.SetTop(vb, ypos);
curField.scaleViewbox = vb;
SelectedFields.Add(curField);
By first wrapping the textblock in a grid, and then wrapping the resulting grid in a viewbox. We get an initial 1:1 scale. Then by changing the dimensions of the viewbox, we can get the resulting fill that is desired. The one thing that I wish I could do though, but cannot find a way, is to center the resulting text inside of the scaled viewbox. Right now, it comes out left aligned even if the viewbox has space to the left. I'm not sure what the cause of this is.
There is not really a concept of vertical centering with a vertically-oriented StackPanel
, nor horizontal centering for a horizontally-aligned StackPanel
.
A StackPanel
only occupies as much space as its constituent elements.
You may need to look at other layout panels for your requirements.
It sounds like you might be able to center the StackPanel
itself inside a Grid
to achieve the centering, but this does not solve the requirement to grow text to fill available space.
The StackPanel stacks vertically, its size in that direction is dependent on the children, if all children were to be centered vertically they would overlap, the whole concept of vertical alignment in a stack does not make sense. You probably want to set the VerticalAlignment
of the StackPanel itself to Center
.
StackPanel starts arranging control from top
Set the StackPanel height to Auto and then set StackPanel's VerticalAlignmet to "Center"
精彩评论