I’m creating a custom WPF control which derives from Grid.
It’s a Chess board and so I want it’s width to be the same as it’s height.
How can I accomplish this?
Edit:
I tried the following.
private void cbcBoard_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (e.NewSize.Width != e.NewSize.Height)
{
double m = Math.Min(e.NewSize.Width, e.NewSize.Height);
cbcBoard.Width = m;
cbcBoard.Height = m;
}
}
It didn’t work. Any ideas?
Thanks.
New solution/workaround. The UserControl can stay as it is, we’ll leave the scaling to the parent container.
We can’t accomplish Min(Width, Height) with just the UserControl because if we set the Height for it, then the parent container won’t scale it Verticaly and the same goes for Width. If we try to juggle them around then there are situations where we end in an endless Width/Height resizing loop.
What we need is another hidden control in the same space that fills it completely and can tell us what its Width and Height is everytime it changes. Then we can use the Math.Min(Width, Height) solution. Something like this. Notice how both controls are in Grid.Row=”1″ and Grid.Column=”1″.
And then in the availableSpace_SizeChanged EventHandler
Now we have 1:1 ratio of the UserControl and it will scale both Vertically and Horizontally