“Cascade” binding on ObservableCollection, containing other ObservableCollection

后端 未结 1 540
情书的邮戳
情书的邮戳 2021-01-27 10:41

I have a project where I need to display a list of contract (Class Affaire). Each contract has a list of phases (Class Phase). I display each of them in 2 different ListView usi

1条回答
  •  北荒
    北荒 (楼主)
    2021-01-27 11:33

    Here is the example, based on your source, that will handle all stuff. a View Models:

    using System.Collections.ObjectModel; using System.ComponentModel;

    namespace WpfApp2 { public class BaseViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string nomPropriete)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(nomPropriete));
        }
    }
    
    public class Contexte : BaseViewModel
    {
        private Affaire _selectedAffaire;
        private Phase _selectedPhase;
        public ObservableCollection ListeDesAffaires { get; set; }
    
        public Affaire SelectedAffaire
        {
            get { return _selectedAffaire; }
            set
            {
                _selectedAffaire = value;
                this.NotifyPropertyChanged("SelectedAffaire");
            }
        }
    
        public Phase SelectedPhase
        {
            get { return _selectedPhase; }
            set
            {
                _selectedPhase = value;
                this.NotifyPropertyChanged("SelectedPhase");
            }
        }
    
        public Contexte()
        {
            ListeDesAffaires = new ObservableCollection
            {
                new Affaire("Affaire1"),
                new Affaire("Affaire2")
            };
        }
    }
    
    public class Affaire : BaseViewModel
    {
        private string nom;
        public string Nom
        {
            get { return this.nom; }
            set
            {
                this.nom = value;
                this.NotifyPropertyChanged("Nom");
            }
        }
    
        public ObservableCollection ListPhases { get; set; }
    
        public Affaire(string n)
        {
            nom = n;
            ListPhases = new ObservableCollection
            {
                new Phase { NomPhase = nom + "_Phase1" },
                new Phase { NomPhase = nom + "_Phase2" }
            };
        }
    }
    
    public class Phase : BaseViewModel
    {
        private string nomPhase;
        public string NomPhase
        {
            get { return this.nomPhase; }
            set
            {
                this.nomPhase = value;
                this.NotifyPropertyChanged("NomPhase");
            }
        }
    
        public ObservableCollection ListAssemblages { get; set; }
    }
    
    public class Assemblage : BaseViewModel
    {
        private string nom;
        public string Nom
        {
            get { return this.nom; }
            set
            {
                this.nom = value;
                this.NotifyPropertyChanged("Nom");
            }
        }
    
    }
    

    }

    a MainWindow.xaml:

    
        
            
        
        
            
                
                
                
            
            
            
            
            
        
    
    

    And some code from your question (MainWindow.xaml.cs):

    using System.Collections.ObjectModel;
    using System.Windows;
    
    namespace WpfApp2
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void FillClick(object sender, RoutedEventArgs e)
            {
                Phase ph = new Phase();
                ph.NomPhase = "SomeId";
                ph.ListAssemblages = new ObservableCollection()
                {
                    new Assemblage { Nom =  "Assemblage1" },
                    new Assemblage { Nom =  "Assemblage2" }
                };
                Contexte.SelectedPhase = ph;
            }
        }
    }
    

    And here is the result:

    This is basic sample that you can extend. It handles all field modification and Add/Remove objects and displays on screen. There is no additional code in MainWindow.xaml.cs. Please ask questions, if any.

    0 讨论(0)
提交回复
热议问题