Correct way to add and remove user controls on a panel.
I have some doubts about this:
- It is right (better way) to do it this way?
- Leave some waste in memory to run the application after a while?
Currently I have a main window as follows:
<!-- MainWindow.xaml -->
<Window x:Class="Empresas.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Empresas" Height="480" Width="640"
MinHeight="480" MinWidth="640">
<DockPanel HorizontalAlignment="Stretch" Width="Auto" Margin="0">
<!-- Menu -->
<Menu x:Name="MainWindowClientesMenu" Width="Auto" Height="25"
DockPanel.Dock="Top">
<MenuItem Header="_Archivo">
<MenuItem Header="Agregar _Nueva empresa" x:Name="MainWindowClientesAgregarEmpresa" Click="MainWindowClientesAgregarEmpresa_Click" />
<Separator/>
<MenuItem Header="Salir"/>
</MenuItem>
</Menu>
<!-- Fin Menu -->
<!-- Barra de Estado -->
<StatusBar x:Name="MainWindowClientesStatusBar" Width="Auto" Height="25"
DockPanel.Dock="Bottom" Background="#ddd" HorizontalAlignment="Stretch">
<StatusBarItem x:Name="MainWindowClientesCurrentAction" HorizontalContentAlignment="Right"
Margin="0 0 5 0"></StatusBarItem>
</StatusBar>
<StackPanel x:Name="MainWindowClientesContenido"></StackPanel>
<!-- Fin Lista isquierda de Empresas/Clientes -->
</DockPanel>
</Window>
My user control is as follows
<!-- NuevaEmpresa.xaml -->
<UserControl x:Class="Empresas.View.NuevaEmpresa"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Height="300" Width="350">
<Grid>
<Label Content="Razon Social" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="118,14,0,0" Name="NuevaEmpresaRazonSocialTxtBox" VerticalAlignment="Top" Width="220" />
<Label Content="Nit" Height="28" HorizontalAlignment="Left" Margin="12,52,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="118,54,0,0" Name="NuevaEmpresaNitTxtBox" VerticalAlignment="Top" Width="220" />
<Button Content="Crear Empresa" Height="23" HorizontalAlignment="Left" Margin="223,114,0,0" Name="NuevaEmpresaCrearButton" VerticalAlignment="Top" Width="115" Click="NuevaEmpresaCrearButton_Click" />
<Button Content="Cancelar" Height="23" HorizontalAlignment="Left" Margin="93,114,0,0" Name="NuevaEmpresaCancelarButton" VerticalAlignment="Top" Width="115" Click="NuevaEmpresaCancelarButton_Click" />
</Grid>
</UserControl>
and how they add and remove the control is as follows:
// MainWindow.xaml.cs
private void MainWindowClientesAgregarEmpresa_Click(object sender, RoutedEventArgs e)
{
MainWindowClientesContenido.Children.Add(new View.NuevaEmpresa(MainWindowClientesContenido));
}
// NuevaEmpresa.xaml.cs
public partial class NuevaEmpresa : UserControl
{
// ...
private Panel _parent;
public NuevaEmpresa(Panel parent)
{
InitializeComponent();
_parent = parent;
// ...
}
// ...
private void NuevaEmpresaCancelarButton_Click(object sender, RoutedEventArgs e)
{
_parent.Children.Clear();
}
}
I don’t see why this could be wrong. You’re setting its child and then clearing it. It seems pretty sane to me. Also don’t see why memory could be leaked. Once the parents clear its children then it must be disposed.