防止按钮单击事件

防止按钮单击事件

我是xaml和c#的新手,如果ProductID已经存在,如何防止按钮单击事件将新行添加到Listview中

private void btnAddToOrder_Click(object sender, RoutedEventArgs e)
{
    //addtoorders
    if (listStocks.SelectedIndex >= 0)
    {
        DataRowView row = (DataRowView)listStocks.SelectedItems[0];

        List<Orders> orders = new List<Orders>();
        orders.Add(new Orders()
        {
            ProductID = row["productID"].ToString(),
            ProductName = row["productName"].ToString(),
            Price = Convert.ToDouble(row["sellingPrice"]),
            Quantity = Convert.ToInt32(row["quantity"])

        });
        listOrders.Items.Add(orders);
      }
 }

最佳答案

private void btnAddToOrder_Click(object sender, RoutedEventArgs e)
    {


        //addtoorders
        if (listStocks.SelectedIndex >= 0)
        {
            DataRowView row = (DataRowView)listStocks.SelectedItems[0];

            Orders newOrder = new Orders()
            {
                ProductID = row["productID"].ToString(),
                ProductName = row["productName"].ToString(),
                Price = Convert.ToDouble(row["sellingPrice"]),
                Quantity = Convert.ToInt32(row["quantity"])

            };
            List<Orders> orders = new List<Orders>();
            //
            bool found = false;
            foreach(Orders order in orders)
            {
                if (order.Equals(newOrder))
                {
                    //if Found Order Equal NewOrder
                    found = true;
                    break;
                }
            }
            if(found == true)
            {
                Console.WriteLine("We are Found Order = NewOrder");
                return; //Exit From Method
            }
            //If Found == false
            orders.Add(newOrder);
            listOrders.Items.Add(orders);
        }
    }

关于c# - 防止按钮单击事件,根据情况将行添加到Listview,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38814531/

10-11 05:48