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)
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)
Model>DataService.cs
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;
}
}
Model>IDataService.cs
public interface IDataService
{
ObservableCollection<Customers> GetCustomers();
}
ModelView>MainViewModel.cs
public class MainViewModel: ViewModelBase
{
readonly IDataService _serviceProxy;
ObservableCollection<Customers> _customers = new ObservableCollection<Customers>();
public ObservableCollection<Customers> Customers
{
get { return _customers; }
set
{
_customers = value;
RaisePropertyChanged();
}
}
public RelayCommand FillTreeView { get; set; }
void GetCustomers()
{
Customers.Clear();
foreach (var item in _serviceProxy.GetCustomers())
{
Customers.Add(item);
}
}
public MainViewModel(IDataService serviceProxy)
{
_serviceProxy = serviceProxy;
Customers = new ObservableCollection<Customers>();
FillTreeView = new RelayCommand(GetCustomers);
}
}
View>TreeView.cs
<Page
.... DataContext="{Binding Main, Source={StaticResource ViewModelLocator}}">
<TreeView x:Name="TreeViewCustomers" Margin="0,23,0,0" Grid.Row="0" ItemsSource="{Binding Customers}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate>
<TextBlock Text="{Binding CustomerName}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<TextBox VerticalAlignment="Top" Margin="0,0,0,270" Text="" Height="23" Grid.Row="0">
<TextBox.InputBindings>
<KeyBinding Key="Return" Command="{Binding FillTreeView}"/>
</TextBox.InputBindings>
</TextBox>