C# 如何使用WPF在listBox.clear()之后显示listBox中的条目?

C# 如何使用WPF在listBox.clear()之后显示listBox中的条目?,c#,listbox,wbf,C#,Listbox,Wbf,我是C#的新手,我正在尝试创建一个库存管理系统 我设法让程序运行的方法是在两个列表框中填入示例名称和相应的存储箱。通过单击“Addsampletostorage”按钮,它将两个列表项保存在一个数组或向量中(我不知道这是什么,我只知道Python的语法),并将其添加到现有的txt文件中。然后它清除列表框,以确保我正确保存了新样本及其存储箱 我的问题: 我希望显示列表框中的项目。由于listBox.clear()的原因,不会发生这种情况。你知道怎么做吗 我感谢你的帮助 main window.xam

我是C#的新手,我正在尝试创建一个库存管理系统

我设法让程序运行的方法是在两个列表框中填入示例名称和相应的存储箱。通过单击“Addsampletostorage”按钮,它将两个列表项保存在一个数组或向量中(我不知道这是什么,我只知道Python的语法),并将其添加到现有的txt文件中。然后它清除列表框,以确保我正确保存了新样本及其存储箱

我的问题:

我希望显示列表框中的项目。由于listBox.clear()的原因,不会发生这种情况。你知道怎么做吗

我感谢你的帮助


main window.xaml

<Window x:Class="OMD_Inventory_Management_System.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:OMD_Inventory_Management_System"
    mc:Ignorable="d"
    Title="OMD Inventory Management System" Height="450" Width="800">

    <Grid Margin="10">

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <ListBox Grid.Row="1" Grid.Column="0" x:Name="lstSample" />
        <ListBox Grid.Row="1" Grid.Column="1" x:Name="lstBin" />

        <StackPanel Grid.Row="1" Grid.Column="2" Margin="5,0,0,0">
            <Label>Sample name:</Label>
            <TextBox x:Name="txtSampleName" />
            <Label>Storage bin:</Label>
            <TextBox x:Name="txtStorageBin" />
            <Button x:Name="Button" Margin="0,5,0,0" Click="ButtonAddName_Click">Add sample to storage</Button>
        </StackPanel>

    </Grid>
</Window>
<Window x:Class="OMD_Inventory_Management_System.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:OMD_Inventory_Management_System"
mc:Ignorable="d"
Title="OMD Inventory Management System" Height="450" Width="800">

<Grid Margin="10">

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <ListBox Grid.Row="1" Grid.Column="0" x:Name="lstSample" />
    <ListBox Grid.Row="1" Grid.Column="1" x:Name="lstBin" />

    <StackPanel Grid.Row="1" Grid.Column="2" Margin="5,0,0,0">
        <Label>Sample name:</Label>
        <TextBox x:Name="txtSampleName" />
        <Label>Storage bin:</Label>
        <TextBox x:Name="txtStorageBin" />
        <Button x:Name="Button" Margin="0,5,0,0" Click="ButtonAddName_Click">Add sample to storage</Button>
        <Button x:Name="Button1" Margin="0,5,0,0" Click="ButtonView_Click">View</Button>
        <Button x:Name="Button2" Margin="0,5,0,0" Click="ButtonClean_Click">Clean</Button>
        <Button x:Name="Button3" Margin="0,5,0,0" Click="ButtonDel_Click">Delete All</Button>
    </StackPanel>

</Grid>

main window.xaml

<Window x:Class="OMD_Inventory_Management_System.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:OMD_Inventory_Management_System"
    mc:Ignorable="d"
    Title="OMD Inventory Management System" Height="450" Width="800">

    <Grid Margin="10">

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <ListBox Grid.Row="1" Grid.Column="0" x:Name="lstSample" />
        <ListBox Grid.Row="1" Grid.Column="1" x:Name="lstBin" />

        <StackPanel Grid.Row="1" Grid.Column="2" Margin="5,0,0,0">
            <Label>Sample name:</Label>
            <TextBox x:Name="txtSampleName" />
            <Label>Storage bin:</Label>
            <TextBox x:Name="txtStorageBin" />
            <Button x:Name="Button" Margin="0,5,0,0" Click="ButtonAddName_Click">Add sample to storage</Button>
        </StackPanel>

    </Grid>
</Window>
<Window x:Class="OMD_Inventory_Management_System.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:OMD_Inventory_Management_System"
mc:Ignorable="d"
Title="OMD Inventory Management System" Height="450" Width="800">

<Grid Margin="10">

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <ListBox Grid.Row="1" Grid.Column="0" x:Name="lstSample" />
    <ListBox Grid.Row="1" Grid.Column="1" x:Name="lstBin" />

    <StackPanel Grid.Row="1" Grid.Column="2" Margin="5,0,0,0">
        <Label>Sample name:</Label>
        <TextBox x:Name="txtSampleName" />
        <Label>Storage bin:</Label>
        <TextBox x:Name="txtStorageBin" />
        <Button x:Name="Button" Margin="0,5,0,0" Click="ButtonAddName_Click">Add sample to storage</Button>
        <Button x:Name="Button1" Margin="0,5,0,0" Click="ButtonView_Click">View</Button>
        <Button x:Name="Button2" Margin="0,5,0,0" Click="ButtonClean_Click">Clean</Button>
        <Button x:Name="Button3" Margin="0,5,0,0" Click="ButtonDel_Click">Delete All</Button>
    </StackPanel>

</Grid>

您应该将MVVM模式与绑定机制结合使用