在 WPF 上的面板上添加和删除用户控件的正确方法

本文关键字:控件 用户 删除 方法 添加 WPF | 更新日期: 2023-09-27 18:32:11

面板上添加和删除用户控件的正确方法。

我对此有些怀疑:

  1. 这样做是对的(更好的方法)吗?
  2. 内存中留下一些浪费以在一段时间后运行应用程序?

目前我有一个主窗口如下:

<!-- 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();
    }
}

在 WPF 上的面板上添加和删除用户控件的正确方法

我不明白为什么这可能是错误的。您正在设置其子项,然后清除它。这对我来说似乎很理智。也不明白为什么内存会泄露。一旦父母清除了孩子,就必须将其处理掉。