Using MVVM Light, When binding my UserControl's DataContext to a ViewModel property within my main ViewModel, the UI won't reflect anything.
Following many of the other answers I found on stack overflow for holding a reference to a child component's ViewModel, I went ahead and created a property within my main ViewModel holding a reference to my child component's ViewModel. I then attempted to bind the child component's DataContext to this property, but it isn't working.
It works when I assign the DataContext in the child component's xaml or code behind to a new instance of the ViewModel.
Main view's xaml (omitting the top):
<mah:HamburgerMenu.ItemsSource>
<mah:HamburgerMenuItemCollection>
<mah:HamburgerMenuIconItem Label="Test">
<mah:HamburgerMenuIconItem.Tag>
<UserControl>
<Controls:IntegrationView DataContext="{Binding IntegrationViewModel}"/>
</UserControl>
</mah:HamburgerMenuIconItem.Tag>
</mah:HamburgerMenuIconItem>
</mah:HamburgerMenuItemCollection>
</mah:HamburgerMenu.ItemsSource>
Child component's xaml (omitting the top):
<StackPanel>
<Label Content="{Binding Text}" />
</StackPanel>
MainViewModel.cs:
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
ChildViewModel = new ChildViewModel();
}
public ChildViewModel ChildViewModel { get; set; }
}
ChildViewModel.cs:
public class ChildViewModel : ViewModelBase
{
private string _text = "Testing string";
public ChildViewModel()
{
}
public string Text
{
get => _text;
set => Set(() => Text, ref _text, value);
}
}
I'm expecting the test string to reflect on the UI, but it doesn't when set the DataContext this way.