2017-05-11 23 views
1

私はを使用してファイルからオブジェクトのリストを消化し、別のウィンドウにリストを渡すことができるフォームを作成するプロジェクトを手配しました。私は、このウィンドウウィンドウ間のオブジェクトの受け渡しC#

public InventoryWindow() 
    { 
     InitializeComponent(); 

     categoryComboBox.Items.Add("All"); 
     categoryComboBox.Items.Add("Pizza"); 
     categoryComboBox.Items.Add("Burger"); 
     categoryComboBox.Items.Add("Sundry"); 
     categoryComboBox.SelectedValue = "All"; 
    } 

    private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 

    } 

    private void categoryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 

    } 
} 

}上に移動する必要が

public class Food 
{ 
    public string Name; 
    public string Category; 
    public int Price; 
} 

public class Ingredient 
{ 
    public string Name; 
    public string Category; 
    public decimal PricePerUnit; 
    public decimal Quantity; 

    public Ingredient(string pName, string pCategory, decimal pPricePerUnit, decimal pQuantity) 
    { 
     Name = pName; 
     Category = pCategory; 
     PricePerUnit = pPricePerUnit; 
     Quantity = pQuantity; 
    } 
} 

/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     List<Ingredient> Inventory = CallInventoryFile(); 
    } 



    private void inventoryButton_Click(object sender, RoutedEventArgs e) 
    { 
     InventoryWindow wnd = new InventoryWindow(); 
     wnd.ShowDialog(); 
    } 

    public List<Ingredient> CallInventoryFile() 
    { 

     List<Ingredient> ProcessedInventory = new List<Ingredient>(); 

     try 
     { 
      string[] fileLines = File.ReadAllLines("Inventory.txt"); 


      //Reading in the file 
      for (int i = 0; i < fileLines.Length; i++) 
      { 
       string[] CurrentLine = fileLines[i].Split(','); 
       string Name = CurrentLine[0].Trim(); 
       string Category = CurrentLine[1].Trim(); 
       decimal PricePerUnit = decimal.Parse(CurrentLine[2].Trim()); 
       decimal Quantity = decimal.Parse(CurrentLine[3].Trim()); 
       Ingredient IngredientToAdd = new Ingredient(Name, Category, PricePerUnit, Quantity); 
       ProcessedInventory.Add(IngredientToAdd); 
      } 
      return ProcessedInventory; 
     } 
     catch 
     { 
      //if we get here read in failed 
      MessageBox.Show("There was an error reading in the file"); 
      return ProcessedInventory; 
     } 
    } 

私の質問は、私はInventoryWindowMainWindowからInventoryの結果を渡すことができる方法です。

あなただけのコンストラクタ内で渡すことができます

答えて

5

、その後

InventoryWindow wnd = new InventoryWindow(Inventory); 
    wnd.ShowDialog(); 

、おそらく行くには最高の方法です

public InventoryWindow(List<Ingredient> inputList) 
{ 
} 
+0

。私はpersonnaly 公共リストを二ウィンドウで成分をパブリックプロパティを作成{取得;セット;}でしょう してから、この InventoryWindow WND =新しいInventoryWindow()のような呼び出しを行う{ 成分= inputList } それはちょうどです人の意見 –

関連する問題