-2

So i have this Collection inside my ViewModel:

public class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private ObservableCollection<NetworkInterface> _interfaces;

        public ViewModel()
        {
            Interfaces = NetworkInterface.ReadAll();
        }

        public ObservableCollection<NetworkInterface> Interfaces
        {
            get { return _interfaces; }
            set
            {
                _interfaces = value;
                NotifyPropertyChanged();
            }
        }

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

This NetworkInterface is implement PropertyChanged and when the application start its full with items.

Initialize my ViewMode:

public MainWindow()
{
     InitializeComponent();
     viewModel = new ViewModel();
 }

I have another Page in my application that load inside Frame after button Click:

<Grid Name="GridMain" Grid.Row="1">
          <Frame Name="MyFrame"
                 NavigationUIVisibility="Hidden"
                 Source="Pages/home.xaml"/>
</Grid>

Load the Page:

Home home = new Home();
MyFrame.Content = home;

And inside my ComboBox:

<ComboBox ItemsSource="{Binding Interfaces}" Height="30" Width="300"/>

I also try:

<ComboBox ItemsSource="{Binding Path=Interfaces}" Height="30" Width="300"/>

But still my ComboBox is empty

EDIT

If this ComboBox is inside the main application and not inside Page this works fine.

user979033
  • 5,430
  • 7
  • 32
  • 50

1 Answers1

0

The DataContext of the MainWindow is not the DataContext of the Page in the Frame. You may set the DataContext of the Frame programmatically though:

The Page in the Frame doesn't automatically inherit the DataContext from the MainWindow, but you can set its DataContext property when you load the Page:

Home home = new Home();
home.DataContext = viewModel;
MyFrame.Content = home;
mm8
  • 163,881
  • 10
  • 57
  • 88