0

I am trying to create a simple dialog-type control that is based on the WPF Window class (Popup won't do the trick here). In my app I register a DataTemplate in Application.Resources:

<Application.Resources>
    <DataTemplate  DataType="{x:Type local:EntitySelectorViewModel}">
        <local:EntitySelector></local:EntitySelector>
    </DataTemplate>        
</Application.Resources>

In my Window control I set Window.Content and I expect WPF will set the ContentTemplate to an instance of EntitySelector based on the DataTemplate registration shown above:

[Export(typeof(EntitySelectorDialog))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class EntitySelectorDialog : Window
{
    [ImportingConstructor]
    public EntitySelectorDialog(EntitySelectorViewModel vm)
    {
        InitializeComponent();

           // DataContext = vm;  // does not work

           // EDIT: Per two answers shown below the following should work but it does not.
           Content = vm;  
    }
}

The problem is that WPF does not resolve the ContentTemplate i.e. an instance of EntitySelector is not created. Furthermore, when I look at the XAML for EntitySelectorDialog I see that an instance of the shell has been injected into the Window control (EntitySelectorDialog).

I don't know enough about Prism to know if I want to go with the flow and use the shell somehow or if I want to prevent Prism from injecting it at all. I don't think I have any need for it in this specific control so if it makes sense to just prevent Prism from injecting it I would prefer that route.

For the record I have tried removing the Prism attributes from my Window control and I new up the components manually. That appears to have no effect - Prism still manages to somehow inject the shell and my ContentTemplate is not resolved.

There is no XAML to show for the Window control (EntitySelectorDialog) except the Window declaration itself - I want the content to come entirely from the ContentTemplate (EntitySelector).

I've looked at this which may provide an answer but I don't know how to implement it without breaking the rest of the app:

Getting Unity to Resolve views in XAML

  • What prevents you from using the `ViewModelLocator`? Why MEF - Unity or any other real DI container is better suited? What does the code look like that creates and shows the window? – Haukinger Aug 23 '18 at 19:41

2 Answers2

0

You need to set vm to EntitySelectorDialog.Content to trigger WPF solve the DataTemplate according to the type. So you either add

Content = vm;

in constructor or add

Content = {Bing}

in Xaml.

Alex.Wei
  • 1,798
  • 6
  • 12
  • Thanks Alex - you are correct of course however for WPF is not cooperating. It does not work. –  Aug 24 '18 at 13:26
0

Set the Content of the window to a ContentControl and set or bind the Content property of this one to the view model:

[Export(typeof(EntitySelectorDialog))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class EntitySelectorDialog : Window
{
    [ImportingConstructor]
    public EntitySelectorDialog(EntitySelectorViewModel vm)
    {
        InitializeComponent();
        DataContext = vm;
        Content = new ContentControl() { Content = vm };
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks I think your answer is correct but it still does not work. –  Aug 24 '18 at 13:35
  • What happens? What do you see in the window? – mm8 Aug 24 '18 at 13:35
  • I see nothing. An instance of of the view (EntitySelector) is not created. –  Aug 24 '18 at 14:31
  • What if you set the Content to a TextBlock? Do you see it? – mm8 Aug 24 '18 at 14:31
  • And you have confirmed that you are actually getting a EntitySelectorViewModel in the constructor of the window? – mm8 Aug 24 '18 at 14:47
  • Yes I have confirmed. –  Aug 24 '18 at 14:51
  • What if you put some TextBlock in your DataTemplate. Are you seeing it? If you don't, you know that the template is not being applied. Try to define it in a ResourceDictionary that you merge into the window's Resources property. – mm8 Aug 24 '18 at 14:52
  • Thanks for these troubleshooting steps! I moved the data template to Window.Resources and it works. It is interesting to note that other controls with DataTemplates defined in the same ResourceDictionary are working fine -however that is a subject for another question. Please set your answer accordingly and I will accept it. –  Aug 24 '18 at 15:24