C# WPF - Adding child after exiting fullscreen is adding it on right of where it was
I'm just starting WPF so am just learning but I've ran into a problem when making a UIElement fullscreen.
I have a horizontal StackPanel
with 2 UIElements inside: 2 more StackPanel
s, the one on the left contains a 3dViewport and another StackPanel
of Button
s and the one on the right contains a vertical stackpanel of Rectangle
s.
When I make the left StackPanel
fullscreen, it works fine, I remove it from the outer StackPanel
and add it as the content of the Window as shown below in the codebehind:
private void mediaFullscreen(object sender, RoutedEventArgs e)
{
containerPanel.Children.Remove(leftPanel);
this.Content = leftPanel;
this.WindowState = WindowState.Maximized;
this.WindowStyle = WindowStyle.None;
isFullscreen = true;
}
The problem is, when I exit fullscreen, because I re-add the left StackPanel
to the outer StackPanel
, it is added at the end of the list/tree and so appears to the right of the (former) right StackPanel
(which holds the Rectangle
s) rather than on the left as it was before fullscreen. Here is the code I use to exit fullscreen:
private void keyDownHandler(object sender, KeyEventArgs e)
{
if (isFullscreen)
{
this.Content = Root;
containerPanel.Children.Add(leftPanel);
this.WindowState = WindowState.Normal;
this.WindowStyle = WindowStyle.SingleBorderWindow;
isFullscreen = false;
}
}
Is there a way to add a child at a certain position so that the (former) left StackPanel
gets put on the left again rather than after the (form开发者_运维知识库er) right StackPanel on the right?
Thanks for your time,
Infinitifizz
Okay just sorted it, sorry for posting the question but I'll answer it here for anyone else who might have the same problem.
Instead of using:containerPanel.Children.Add(leftPanel);
when coming out of fullscreen, I've used: containerPanel.Children.Insert(0, leftPanel);
which inserts the leftPanel element as the first index of the containerPanel, therefore it is at the left like it used to be before going fullscreen.
Sorry for wasting your time,
InfinitiFizz
精彩评论