0

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.

Coffey
  • 11
  • 1
  • 4
  • Are you setting the `DataContext` of `ChildView` somewhere else? What's the `DataContext` of the `MainView`? – mm8 Jul 18 '19 at 15:23
  • Are there any data binding error messages in the Output Window in Visual Studio? I'd also bet that you have set the ChildView's DataContext explicitly, e.g. in its constructor or XAML. See here: https://stackoverflow.com/a/28982771/1136211 – Clemens Jul 18 '19 at 15:25
  • @mm8 Confirmed that I'm not setting the ChildView's DataContext anywhere else. I'm setting the MainView's DataContext in it's xaml, toward the top, which is working. – Coffey Jul 18 '19 at 15:26
  • @Clemens yes there are error messages in the output, actually. I have the control nested within a HamburgerMenu, which I omitted, but that might be the problem? `System.Windows.Data Error: 40 : BindingExpression path error: 'ChildViewModel' property not found on 'object' ''HamburgerMenuIconItem' (HashCode=33785274)'. BindingExpression:Path=ChildViewModel; DataItem='HamburgerMenuIconItem' (HashCode=33785274); target element is 'ChildView' (Name=''); target property is 'DataContext' (type 'Object')` – Coffey Jul 18 '19 at 15:32
  • Given that the current DataContext holds an instance of MainViewModel when you set `DataContext="{Binding ChildViewModel}"`, and the ChildView's DataContext isn't set somewhere else (also not in the control's XAML), your code should work. We need more context. – Clemens Jul 18 '19 at 15:32
  • Ok, what is HamburgerMenuIconItem? That is obviousy the object in the current DataContext which is used by `{Binding ChildViewModel}`. – Clemens Jul 18 '19 at 15:32
  • It's coming from the MahApps Metro UI framework, I'll edit the post above to show the full thing – Coffey Jul 18 '19 at 15:34
  • You may need to write `DataContext="{Binding DataContext.ChildViewModel, RelativeSource={RelativeSource AncestorType=Window}}"` when you are inside of some ItemsControl. – Clemens Jul 18 '19 at 15:35
  • And why is there suddenly `RelativeSource={RelativeSource Self}`? That makes no sense at all. – Clemens Jul 18 '19 at 15:40
  • Sorry, left that in there from screwing around earlier by mistake. I'm trying out your recommendation now. – Coffey Jul 18 '19 at 15:42
  • Looks like that did it. Oddly if I don't specify `AncestorLevel=1` it doesn't work though. Thanks for the help. – Coffey Jul 18 '19 at 15:46

0 Answers0