各ボタンクリックイベントの後に、新しいアイテムを追加して以前のアイテムを汎用リストに保持するにはどうすればよいですか?

バーズブルー

ユーザーがボタンをクリックしたときにジェネリックリストに新しいアイテムを追加したいのですが、ボタンをクリックすると、リストには最後に導入されたアイテムのみが含まれていることがわかります。各ボタンのクリックリストの間に再初期化されるようです。古いアイテムを保持し、新しいアイテムをジェネリックリストに追加して、それらすべてをリストボックスに表示するにはどうすればよいですか?

ありがとうございました..

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可能な場合は避けてください
  • クリックするたびにリストの新しいインスタンスを作成しないでください。前のアイテムが失われます。ウィンドウでフィールドを宣言します。
  • listBoxは、新しいアイテムがいつ追加されて表示されるかを知る必要があります。しかし、リストは追加/削除について報告しません。使用する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]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ