I have a usercontrol in a content pane. If I use mvvm datatemplate to create my view:
<DataTemplate DataType="{x:Type platevm:PlateViewModel}" >
<plate:PlateView />
</DataTemplate>
The view is instantiated twice if in a content pane. So:
Hello,
Thank you for your post. I have been looking into it and I can say that XamDockManager's Tabs collection is read only and cannot be bound to a collection source, but if you want I can log a product idea for such functionality.
Looking forward for your reply.
I have same problem bit more complicated. I want to bind collection of viewmodel to tabgroup panel.
<Window.Resources>
<DataTemplate DataType="{x:Type local:Foo}">
Excellent help guys! Gold stars for both of you.
Simply putting my viewmodel in <ContentControl> tags solved the problem and only took about 5 minutes to update everything.
Cheers!
In trying this with a new project I do not see the problem you are describing. I can guess as to what is happening. Basically the ContentPane is a ContentControl. When you set the Content property of a ContentControl, it is not the ContentControl itself that searches for and uses the datatemplate - it is the ContentPresenter within its Template. So if the Template of the ContentControl (ContentPane or otherwise) is changed, then the ContentPresenter in the new template must create its own instance of the DataTemplate's content. So I'm guessing that you are doing something that is causing the ContentPane's Template to be changed - e.g. setting the Theme property, changing the OS theme, etc. One thing that may work for you is to just set the Content to be a ContentControl and set the Content of that to be your object. In this way, the template of the containing ContentControl would not be changed and so only 1 instance should be created. e.g.
Of course if you have any code that assumes that the logical parent of your viewmodel is the ContentPane then you would have to change that accordingly.
When you are setting the Content of a Control directly, you should put the element directly. If you want to provide a customization for that content, then you use a Template. I doubt that I can explain this better than the MSDN, so you can take a look this great article here.