This project has moved. For the latest updates, please go here.

Mvvm light Tree view binding with entity framwork

Nov 11, 2016 at 1:00 PM
Edited Nov 11, 2016 at 1:03 PM
Hello, can somebody explane or show simple example with treeview and entity framework?

Why binding not worknig then press enter? What is the best way to show treeview onload?

Sorry for big code sample:

I use Mvvm light Tree with entity framwork. Folders Model, ModelView and View DB.edmx in Model folder (class DBEntity)


public class DataService : IDataService
    readonly DBEntities _context;

    public DataService()
        _context =  new DBEntities();

    public ObservableCollection<Customers> GetCustomers()
        var customers = new ObservableCollection<Customers>(_context.Customers.Take(10));
        return customers;


public interface IDataService
    ObservableCollection<Customers> GetCustomers();


public class MainViewModel: ViewModelBase
    readonly IDataService _serviceProxy;
    ObservableCollection<Customers> _customers = new ObservableCollection<Customers>();

    public ObservableCollection<Customers> Customers
        get { return _customers; }
            _customers = value;
    public RelayCommand FillTreeView { get; set; }

    void GetCustomers()
        foreach (var item in _serviceProxy.GetCustomers())

    public MainViewModel(IDataService serviceProxy)
        _serviceProxy = serviceProxy;
        Customers = new ObservableCollection<Customers>();
        FillTreeView = new RelayCommand(GetCustomers);



      .... DataContext="{Binding Main, Source={StaticResource ViewModelLocator}}">
<TreeView x:Name="TreeViewCustomers" Margin="0,23,0,0" Grid.Row="0" ItemsSource="{Binding Customers}">
                        <TextBlock Text="{Binding CustomerName}"/>
            <TextBox VerticalAlignment="Top" Margin="0,0,0,270" Text="" Height="23" Grid.Row="0">
                    <KeyBinding Key="Return" Command="{Binding FillTreeView}"/>