Rahul W
Rahul W

Reputation: 841

View declared in DataTemplate keeps on creating when changing from Tab to Tab

I have been following MVVM pattern to design my application. However, i have noticed a strange thing. I have a Tab control in which i have specified the ItemsSource as Binding for WorkSpaces from MainViewModel. In the Items, i have been adding different ViewModels. I have defined the DataTemplate for the ViewModel datatypes.

However, when i click on a tab, a new View is created everytime.

Sample code i am posting here.

<Grid.Resources>

<DataTemplate DataType="{x:Type spavm:DashboardViewModel}">
  <UIDashboard:DashboardView/>
</DataTemplate>

</Grid.Resources>

<dx:DXTabControl Grid.Row="2" Margin="5" 
                         Name="MainContentTabs"
                         ItemsSource="{Binding WorkSpaces}"
                         ItemHeaderTemplate="{StaticResource WorkspaceItemTemplate}">
</dx:DXTabControl>

From the above, if i switch to any other tab and revisit my DashboardView, it is getting created again.

Am i missing anything?? I just need a single instance of the DashboardView. How can i achieve that.

Upvotes: 1

Views: 266

Answers (1)

Matt
Matt

Reputation: 2682

As mentioned, this is by design in DevExpress. However, there is a property called DestroyContentOnTabSwitching that you can set to false. DX Documentation.

If your views are expensive to create you could also consider refactoring the expensive logic to cache it.

Upvotes: 2

Related Questions