Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Binding with ObservableCollection and Entity Framework

$
0
0

Hi!

I have came across something that really gets my head spinning. I have created a ListBox to present data in order to Learn a bit about DataBindings. (See code below)

If i try to populate my collection using the ObservableCollection<Family>(f) it doesn't work

But if i add an item at a time it does work.

Any idea? Regards Martin

    public partial class MainWindow : Window
    {
        BFA140820Entities BFA = new BFA140820Entities();

        // ViewModal Class contains:
        // public ObservableCollection<Family> Families {get;set;}
        ViewModel vm = new ViewModel();

        public MainWindow()
        {
            //nl = new NameList();
            //vm.Families = new ObservableCollection<Family>();


            InitializeComponent();
            this.DataContext = vm;

            this.Loaded += MainWindow_Loaded;

        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var f = from family in BFA.Families where family.famPrimaryFirstName == "Martin" orderby family.famPrimaryLastName select family;

            // When doing this, it will not populate the ListBox
            vm.Families = new ObservableCollection<Family>(f);

            // How ever... This will?!?!
            //foreach (var itm in f)
            //{
            //    vm.Families.Add(itm);
            //}
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Family fam = vm.SelectedFamily;
            MessageBox.Show(string.Format("Förnamn {0}, Efternamn {1}", fam.famPrimaryFirstName, fam.famPrimaryLastName));
            fam.famPrimaryFirstName = "Nytt namn";

            //vm.GetNameList.Add(new PersonName() { FirstName = "HAHAH", LastName = "TJOHOI" });
            //nl.Add(new PersonName() { FirstName = "FIRST", LastName = "NAME" });
        }

    }


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>