I developing an application in which I need to integrate scaling of the UI.Althrough it's something trivial to do, I experience some problems. My UI isn't particulary clean and well formated to begin with(the XAML).
I have a Grid which looks like this and acts as a wrapper for all other controls. It has the same height and width as the height & width of the window
<Grid x:Name="parentBox" Margin="10,10,2,0" Height="511" VerticalAlignment="Top">
Then I got a lot of other nested Grid's inside this grid and some other controls.The number of all controls is ~200.
Then I register the SizeChanged event for the "parentBox" named control which I noted above, and I have the following recursive algorithm to scale the controls.
Registered Event -----
private void ParentSizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
{
if (e.PreviousSize.Width != 0 && e.PreviousSize.Height != 0)
{
double coefficientHeight = e.NewSize.Height / e.PreviousSize.Height;
double coefficientWidth = e.NewSize.Width / e.PreviousSize.Width;
Scale(Parent, coefficientWidth, coefficientHeight);
}
}
Function to scale all children.
private void Scale(Grid Parent, double coefficientW, double coefficientH)
{
if (Parent.Children.Count == 0)
{
return;
}
else
{
if (coefficientH > 0 && coefficientW > 0)
{
Parent.Width *= coefficientW;
Parent.Height *= coefficientH;
for (int y = 0; y < Parent.Children.Count; y++)
{
var child = Parent.Children[y];
if (child is Grid)
{
var childG = child as Grid;
Scale(childG, coefficientW, coefficientH);
}
else
{
if (child is ElementRepresentation)
{
var element = child as ElementRepresentation;
element.Width *= coefficientW;
element.Height *= coefficientH;
if (element.Height < 25 || element.Width < 25)
{
if (!ElementNames.ContainsKey(int.Parse(element.elementNumber.Text)))
{
ElementNames.Add(int.Parse(element.elementNumber.Text), element.ElementLongName.Text);
element.ElementLongName.Text = String.Empty;
}
}
if (element.ElementLongName.Text == String.Empty)
{
element.ElementLongName.Text = ElementNames[int.Parse(element.elementNumber.Text)];
}
}
else
{
if(child is Label)
{
var label = child as Label;
label.Width *= coefficientW;
label.Height *= coefficientH;
}
}
}
}
}
}
}
While I debug and I scale the height of the Window the "parentBox" control the SizeChanged event fires only when I scale the window from the width side and nothing occurs when I try to do change the height.I'm suspecting it's something to do with my control placements which are inside the nested inside "parentBox" grids.
I appreciate your time to read this question.Thanks.