我很抱歉,如果這個問題之前已經問過,但我非常接近我的頭,基本上,當我點擊組合框它給了我4個選項來過濾列表框(所有,披薩,漢堡,雜食)披薩,Burger和Sundry是類別名稱中的單詞。我如何製作它,以便我的列表框僅顯示組合框中選定的內容。如何使用組合框過濾我的列表框項目?
class InventoryItem
{
public string CategoryName { get; set; }
public string FoodName { get; set; }
public double Cost { get; set; }
public double Quantity { get; set; }
public override string ToString()
{
return $"{CategoryName} - {FoodName}. Cost: {Cost:0.00}, Quantity: {Quantity}";
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void inventoryButton_Click(object sender, RoutedEventArgs e)
{
InventoryWindow wnd = new InventoryWindow();
//Var filepath allows the code to access the Invenotry.txt from the bin without direclty using a streamreader in the code
var filePath = "inventory.txt";
if (File.Exists(filePath))
{
// ReadAllLines method can read all the lines from the inventory.text file and then returns them in an array, in this case the InventoryItem
var fileContents = File.ReadAllLines(filePath);
foreach (var inventoryLine in fileContents)
{
// This makes sure our line has some content with a true or false boolean value, hence continue simply allows the code to continue past the if statment
if (string.IsNullOrWhiteSpace(inventoryLine)) continue;
//We can now Split a line of text in the inventory txt into segments with a comma thanks to the inventoryLine.Split
var inventoryLineSeg = inventoryLine.Split(',');
var inventoryItem = new InventoryItem();
// if the code was succesful in trying to parse the text file these will hold the value of cost and quantity
double cost;
double quantity;
// Assign each part of the line to a property of the InventoryItem
inventoryItem.CategoryName = inventoryLineSeg[0];
if (inventoryLineSeg.Length > 1)
{
inventoryItem.FoodName = inventoryLineSeg[1];
}
if (inventoryLineSeg.Length > 2 & double.TryParse(inventoryLineSeg[2], out cost))
{
inventoryItem.Cost = cost;
}
if (inventoryLineSeg.Length > 3 & double.TryParse(inventoryLineSeg[3], out quantity))
{
inventoryItem.Quantity = quantity;
}
//Now able to add all the InventoryItem to our ListBox
wnd.ListBox.Items.Add(inventoryItem);
}
wnd.ShowDialog();
}
}
private void foodMenuButton_Click(object sender, RoutedEventArgs e)
{
FoodMenuWindow wnd = new FoodMenuWindow();
wnd.ShowDialog();
}
private void newOrderButton_Click(object sender, RoutedEventArgs e)
{
OrderWindow wnd = new OrderWindow();
wnd.ShowDialog();
}
private void completedOrdersButton_Click(object sender, RoutedEventArgs e)
{
CompletedOrdersWindow wnd = new CompletedOrdersWindow();
wnd.ShowDialog();
}
private void quitButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
做了'串的快速搜索。 Split()函數,它的重載簡單修復 – MethodMan
你的foreach在最後有一個問題......你正在迭代'DLL'中的項目(每個項目都是'x'),但是在循環中你不斷地添加相同的「線路」。你應該添加'x'。 –