Hello. There is a WPF MVVM window. There is a DataGrid in the window, one of the columns in the grid contains a ListBox , the data of which the ViewModel must provide.

 <DataGrid Name="OperativeScheduleTable" Grid.Row="1" ItemsSource="{Binding Path= OperativeSchedules}"> <DataGrid.Columns> <DataGridTextColumn Width="Auto" Binding="{Binding Path=Id}" Header="Id" IsReadOnly="True"/> <DataGridTemplateColumn Header="Станция отправления"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ListBox ItemsSource="{Binding Path=Stations}" Padding="5"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Width="50" Text="{Binding Path=Name}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> 

ViewModel:

 public class RailwayStationEditViewModel : Screen { private readonly IUnitOfWork _unitOfWork; public RailwayStation RailwayStations { get; private set; } public ObservableCollection<Station> Stations { get; set; } //Сюда нужно Байндится!!! public RailwayStationEditViewModel(IUnitOfWork unitOfWork, RailwayStation railwayStation) { _unitOfWork = unitOfWork; RailwayStations = railwayStation; Stations = new ObservableCollection<Station>(RailwayStations.Stations.ToList()); } } 

It is necessary to specify most likely through DataContext:

  <ListBox ItemsSource="{Binding Path=Stations, указать что коллекция находится во ViewModel}" Padding="5"> 

And now the search for the Stations collection is done in the OperativeSchedules element ( DataGrid bending)

    1 answer 1

    Give the window a name for which the ViewModel in which the Stations is located.

    Next get attached like this:

     <ListBox ItemsSource="{Binding DataContext.Stations, ElementName=имяОкна}"/> 
    • Fine! everything is working! thank! - Aldmi
    • I wanted to ask another question, I need to edit the cells in the dataGrid, but when I attach directly to ICollection <T>, the essence of the domain model EF, an editing error occurs. and I have to bind to the internal collections of the ViewModel initialized with ICollection <T> values. Is this true? - Aldmi
    • @Aldmi, does the EF model have a VM? And so it is better to ask a separate question. - Gardes
    • Okay, let's ask separately thank you! - Aldmi
    • @Aldmi, please! - Gardes