How to launch the simplest code illustrating Master-Detail (compound List) object databinding in WPF?

后端 未结 2 2047
面向向阳花
面向向阳花 2020-12-22 05:47

I am trying to run (in C# 4.0, Visual Studio 2010) the code from

  • Unable to display a List as one of the columns of my view with ItemsControl and StackPanel?
2条回答
  •  無奈伤痛
    2020-12-22 06:32

    The simpler answer:
    This is funny but I do not see in HichemC's answer edits his penultimate edition of C# code working with the same XAML without any bloating by:

    • ObservableCollection
    • OnPropertyChanged()
    • INotifyPropertyChanged
    • PropertyChangedEventHandler

    That this, simpler, code works for me:

    public partial class MainWindow : Window
    {
        public List myOrders { get; set; }
        Order order1 = new Order
        {
            OrderName = "Order1",
            PartsList = new List()
            {
                  new Parts {PartName = "Part11", PartQuantity = 11},
                  new Parts {PartName = "Part12", PartQuantity = 12}
             }
         };
        Order order2 = new Order
        {
            OrderName = "Order2",
            PartsList = new List()
            {
                  new Parts {PartName = "Part21", PartQuantity = 21},
                  new Parts {PartName = "Part22", PartQuantity = 22},
                  new Parts {PartName = "Part23", PartQuantity = 23}
             }
         };
    
         public MainWindow()
            {
                InitializeComponent();
                myOrders = new List();
                myOrders.Add(order1);
                myOrders.Add(order2);
                DataContext = this;
            }
        }
        public class Order
        {
            public string OrderName { get; set; }
            public List PartsList { get; set; }
        }
    
        public class Parts
        {
            public string PartName { get; set; }
            public double PartQuantity { get; set; }
            }
    }
    

    Master-Detail compound List object Databinding in WPF App- Simplest Possible Realization

提交回复
热议问题