我想在用户单击按钮时将新项目添加到我的通用列表中,但是当我单击按钮时,我看到列表只包含最后引入的项目。似乎在每个按钮单击列表期间都被重新初始化。如何保留旧项目并将新项目添加到通用列表中并在列表框中显示所有项目?
谢谢..
C#代码
namespace Example
{
/// <summary>
/// Interaction logic for CreateProduct.xaml
/// </summary>
public partial class CreateProduct : Window
{
public static float weight;
public static int quantity;
public static string customer, piece, material;
public CreateProduct()
{
InitializeComponent();
}
public static List<Liste> AddList()
{
List<Liste> list = new List<Liste>();
Liste kayit= new Liste();
kayit.Customer = customer;
kayit.Piece = piece;
kayit.Material = material;
kayit.Quantity = quantity;
kayit.Weight = weight;
list.Add(kayit);
return list;
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
customer = btnEditCustomer1.Text;
piece = btnPiece.Text;
material = txtMaterial.Text;
quantity = Convert.ToInt32(txtQuantity.Text);
weight = float.Parse(txtWeight.Text);
if (customer != null && piece != null && material != null)
{
listBoxProduct.ItemsSource = AddList();
}
}
}
public class Liste
{
public string Customer { get; set; }
public string Piece { get; set; }
public string Material { get; set; }
public int Quantity { get; set; }
public float Weight { get; set; }
}
}
XAML 代码
<ListBox Grid.Row="1" x:Name="listBoxProduct" SelectionMode="Single" Margin="0" Background="Transparent" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Height="200">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" Margin="0" Height="30" CornerRadius="4" Width="875" Background="#2E323B" BorderBrush="Black">
<DockPanel>
<TextBlock Text="{Binding Customer}" Foreground="White" TextWrapping="Wrap" VerticalAlignment="Stretch" FontSize="16" HorizontalAlignment="Left" Margin="4,0,0,0"/>
<TextBlock Text="{Binding Piece}" Foreground="White" TextWrapping="Wrap" VerticalAlignment="Stretch" FontSize="16" HorizontalAlignment="Left" Margin="4,0,0,0"/>
<TextBlock Text="{Binding Material}" Foreground="White" TextWrapping="Wrap" VerticalAlignment="Stretch" FontSize="16" HorizontalAlignment="Left" Margin="4,0,0,0"/>
<TextBlock Text="{Binding Quantity}" Foreground="White" TextWrapping="Wrap" VerticalAlignment="Stretch" FontSize="16" HorizontalAlignment="Left" Margin="4,0,0,0"/>
<TextBlock Text="{Binding Weight}" Foreground="White" TextWrapping="Wrap" VerticalAlignment="Stretch" FontSize="16" HorizontalAlignment="Left" Margin="4,0,0,0"/>
</DockPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
修复您的代码的一些问题:
static
尽可能避免ObservableCollection
public partial class CreateProduct : Window
{
private ObservableCollection<Liste> list = new ObservableCollection<Liste>();
public CreateProduct()
{
InitializeComponent();
listBoxProduct.ItemsSource = list;
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
float weight;
int quantity;
string customer, piece, material;
customer = btnEditCustomer1.Text;
piece = btnPiece.Text;
material = txtMaterial.Text;
quantity = Convert.ToInt32(txtQuantity.Text);
weight = float.Parse(txtWeight.Text);
if (customer != null && piece != null && material != null)
{
Liste kayit = new Liste();
kayit.Customer = customer;
kayit.Piece = piece;
kayit.Material = material;
kayit.Quantity = quantity;
kayit.Weight = weight;
list.Add(kayit);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句