0

I'm using Prism/MVVM/WPF and have ViewModels:

public class ViewModelP : BindableBase
{
    private string _stringP;
    public string StringP
    {
        get => _stringP;
        set => SetProperty(ref _stringP, value);
    }

    private ObservableCollection<ViewModelQ> _myQs
    public ObservableCollection<ViewModelQ> MyQs
    {
        get => _myQ;
        set => SetProperty(ref _myQ, value);
    }
}

public class ViewModelQ : BindableBase
{
    private string _stringQ;
    public string StringQ
    {
        get => _stringQ;
        set => SetProperty(ref _stringQ, value);
    }
}

And then I have a View:

<UserControl x:Class="NamespacePath.ViewP"
             xmlns:local="NamespacePath"
             d:DataContext="{d:DesignInstance Type=local:ViewModelP, IsDesignTimeCreatable=False}">
<!--I left out most of the boilerplate declarations from UserControl-->

    <DockPanel>
        <TextBlock Text={Binding StringP}/> <!--This binding works-->
        <TabControl ItemsSource={Binding MyQs}>
            <TabControl.Resources>
                <DataTemplate DataType="{x:Type local:ViewModelQ}">
                    <DockPanel>
                        <TextBlock Text="{Binding StringP}"/> <!--This binding does not work-->
                        <TextBlock Text="{Binding StringQ}"/> <!--This binding works-->
                    </DockPanel>
                </DataTemplate>
            </TabControl.Resources>
        </TabControl>
    </DockPanel>
</UserControl>

I know that ViewModelQ's do not inherently have any reference to ViewModelP, but is it possible to, in the View, bind to the DataContext of the parent control (the User Control)?

4
  • What is ViewModelQ? Why should it be relevant, considering that the TabControl is bound to a collection of Q? Commented Jun 30 at 15:09
  • 1
    Besides that, you can write Text="{Binding DataContext.StringP, RelativeSource={RelativeSource AncestorType=TabControl}}" - if that was your question. Commented Jun 30 at 15:14
  • @Clemens That was a typo. Q and ViewModelQ should be the same thing. The question has been updated Commented Jun 30 at 16:39
  • You asked the question a bit confusingly. In my opinion, this version would be clearer for everyone: "WPF - If ViewModelP has-a ViewModelQ, and the view has a UsrControl ViewP which contains template ViewQ, how can I bind something in ViewQ to ViewModelP?" Commented Jun 30 at 18:48

1 Answer 1

0

1. Variant with ElementName

<UserControl x:Class="NamespacePath.ViewP"
             xmlns:local="NamespacePath"
             d:DataContext="{d:DesignInstance Type=local:ViewModelP,
                                              IsDesignTimeCreatable=False}"
             x:Name="main">
<!--I left out most of the boilerplate declarations from UserControl-->

    <DockPanel>
        <TextBlock Text={Binding StringP}/> <!--This binding works-->
        <TabControl ItemsSource={Binding MyQs}>
            <TabControl.Resources>
                <DataTemplate DataType="{x:Type local:ViewModelQ}">
                    <DockPanel>
                        <TextBlock Text="{Binding  DataContext.StringP,
                                                   ElementName=main}"/> <!--Now this binding will work.-->
                        <TextBlock Text="{Binding StringQ}"/> <!--This binding works-->
                    </DockPanel>
                </DataTemplate>
            </TabControl.Resources>
        </TabControl>
    </DockPanel>
</UserControl>

2. Variant with RelativeSource FindAncestor

        <TextBlock Text="{Binding  DataContext.StringP,
                                   RelativeSource={RelativeSource FindAncestor,
                                                                  AncestorType={x:Type local:ViewP}}}"/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.