在面板上添加和删除用户控件的正确方法。
我对此有一些疑问:
- 这样做是正确的(更好的方法)吗?
- 一段时间后在内存中留下一些浪费来运行应用程序?
目前我有一个主窗口如下:
<!-- 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>
我的用户控件如下
<!-- 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>
以及他们如何添加和删除控件如下:
// 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();
}
}