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

WPF Binding a listbox in XAML

$
0
0

Hi I am banging my head for days now on this, so please, please help.

I have a WPF application with a EF6 Model and a ViewModel. this is my ViewModel:

using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using WpfApplication1.Model; namespace WpfApplication1.ViewModel { public class MainWindowViewModel : INotifyPropertyChanged { public ObservableCollection<Persoon> Personeel;

public MainWindowViewModel() { RoosterDBEntities db = new RoosterDBEntities(); Personeel = new ObservableCollection<Persoon>(); var query = from p in db.Personeel select p; foreach (var item in query) { Persoon p = new Persoon(); p.AchterNaam = item.LastName; p.VoorNaam = item.FirstName; p.KLMID = item.KLMId; Personeel.Add(p); } } public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } public class Persoon { public String VoorNaam { get; set; } public String AchterNaam { get; set; } public int KLMID { get; set; } } }

At first I just want to display something so I do not use PropertyChange jet.

The XAML in my MainWindow :

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:vm="clr-namespace:WpfApplication1.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="1000" Width="1500"><Grid ><ListBox x:Name="lb"   ><ListBox.ItemTemplate><DataTemplate><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="*"/><ColumnDefinition Width="100"/></Grid.ColumnDefinitions><TextBlock Text="{Binding VoorNaam}" /></Grid></DataTemplate></ListBox.ItemTemplate></ListBox></Grid></Window>

MainWindow Code behind:

namespace WpfApplication1
{
        public partial class MainWindow : Window
    {
        private MainWindowViewModel VM;

        public MainWindow()
        {
            InitializeComponent();
            VM = new MainWindowViewModel();
            this.DataContext = VM;
            lb.ItemsSource = VM.Personeel;
        }
    }
}

This works fine. But I like to do this the propper way so I try to Bind in my XAML.

So In code behind I remove the last 3 lines and keep only:

public MainWindow()
        {
            InitializeComponent();
        }

and my XAML I change to:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:vm="clr-namespace:WpfApplication1.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="1000" Width="1500"><Window.DataContext><vm:MainWindowViewModel/></Window.DataContext><Grid ><ListBox x:Name="lb" ItemsSource="{Binding Personeel}"  ><ListBox.ItemTemplate><DataTemplate><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="*"/><ColumnDefinition Width="100"/></Grid.ColumnDefinitions><TextBlock Text="{Binding VoorNaam}" /></Grid></DataTemplate></ListBox.ItemTemplate></ListBox></Grid></Window>

I can see my MainWindowViewModel is instantiating by setting a break point in it. but nothing shows in my MainWindow.

the only strange thing I see is that at the line

<vm:MainWindowViewModel/>

I get a message that "No connection string named 'RoosterDBEntities' could be found in the application config file."

But I checkt App.Config and it is there, and I can see my ObservableCollection Personeel gets filled.

Can somebody please tell me what I am doing wrong?


Rob


Viewing all articles
Browse latest Browse all 18858


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