0
votes

I've a WPF application, with which I use Prism for the architecture.

I've something a little bit custom: I've one UserControl(a wizard), that can receive a FrameworkElement. This element is displayed within the wizard usercontrol with a ContentPresenter.

Basically, the view that will use this usercontrol will have such code:

<UserControl x:Class"My.Instance" 
  //skipping namespaces
  mvvm:ViewModelLocator.AutoWireViewModel="True">
    <Wizard>
        <Wizard.ContentElement>
            <TextBlock Text="{Binding MyInstanceProperty}"/>
        </Wizard.ContentElement>
    </Wizard>
</UserControl>

Within the "Wizard" UserControl, I just have such thing:

<ContentControl Content="{Binding ContentElement}" Margin="10"/>

The context being the code behind of the usercontrol(set on the root Grid).

On runtime, I've the following error

System.Windows.Data Error: 40 : BindingExpression path error: 'MyInstanceProperty' property not found on 'object' ''Wizard' (HashCode=29548405)'. BindingExpression:Path=MyInstanceProperty; DataItem='WizardViewModel' (HashCode=29548405); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

So it seems that my TextBlock has its DataContext set on the Wizard UserControl, but not on the "My.Instance" owner.

I guess, it's because I host it within a ContentPresenter?

How can I avoid this?

1
Maybe you are looking for something like this: stackoverflow.com/a/1127964/5147720 - Jose

1 Answers

0
votes
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type UserControl}}, Path=DataContext.MyInstanceProperty}"/>

EDIT>>>>>

Wizard view:

<ContentControl Content="{Binding ContentElement}" Margin="10"/>

Wizard view model:

ContentElement = new pageViewModel();

App.xaml:

<DataTemplate DataType="{x:Type myViewModel:pageViewModel}">
    <local:pageView/>
</DataTemplate>