0

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?

J4N
  • 19,480
  • 39
  • 187
  • 340
  • Maybe you are looking for something like this: http://stackoverflow.com/a/1127964/5147720 – Jose Sep 16 '15 at 14:38

1 Answers1

0
<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>
Jose
  • 1,857
  • 1
  • 16
  • 34
  • I also found this previously, but can you explain me why my userControl is "loosing" his DataContext? (should not we use the ancestor type `My.Instance`? – J4N Sep 16 '15 at 14:56
  • Also, in my real case, I've several items given to the `Wizard`, only the "current/active" element is hosted inside a `ContentControl`, the other are just contained in a DependencyProperty. When I change the element(=move to the next step of the wizard), I've some binding error, so I've the feeling that this FindAncestor only work when the Control is hosted inside the ContentControl? – J4N Sep 16 '15 at 15:02
  • I've never used that "wizard" and I cant help you with that. When I want to create a view from my viewmodel without breaking the MVVM I always use that: https://rachel53461.wordpress.com/2011/07/17/navigation-with-mvvm/ – Jose Sep 16 '15 at 15:10
  • `Wizard`is a UserControl that I'm currently doing. The issue that I've is that I've a list of "Pages" in my `Wizard` and when I do next, I take the current page +1, and I display it. But when I do this I go an error with the RelativeSource+FindAncestor – J4N Sep 17 '15 at 04:40
  • So, your wizard is the main class. Maybe this be easier. Wizard view: . Wizard viewmodel: ContentElement = new otherUserControlViewModel(); And do the link in App.xaml. I will edit the answer. – Jose Sep 17 '15 at 06:15
  • I cannot do this, I've several FrameworkElement that are bound to the same ViewModel(since most of the pages needs data coming from the other pages), I need to be able to indicate the page X has now to be displayed. And more important, this ViewModel is unknown from the Wizard(the Wizard is generic, it only knows that it has to display some pages, and that each pages can be moved to next or previous). I've several usage of this wizard with totally different context(viewmodel+control) – J4N Sep 17 '15 at 08:12
  • Also, I've prism, everything is within modules, so I cannot have things that are so specific within the app.xaml – J4N Sep 17 '15 at 08:15