0

I have a MVVM trading application.

MainView has two tab controls - left one displays a collection of "workspaces" and the right one displays dynamic data display charts.

When I create the first "chartviewmodel" the correct chart appears in the right tab control as expected. When I then create another "chartviewmodel" with different data that should result in a different chart, that's not what happens. The second tab item is a duplicate of the first from a visual standpoint. The tab items are contained in a collection of items called: "chartspaces" and in debug mode I can see each viewmodel and indeed each has different values.

Also, in debug mode I put a breakpoint on the "Initialize()" method of the view and when the first chartviewmodel is created, that method indeed gets called. When I then create the second chartviewmodel, the "Initialize()" method is not called, so in fact another view is not getting created...but I can't figure out why.

My view/viewmodel's are tied together via DataTemplate with x:Shared="False" (tried it with and without this parameter). I've traced through the constructor of the chartviewmodel to ensure it was not getting any errors and it is not.

On the left-most tab control, I can create multiple tradeviewmodel and each results in a different view... in other words it is behaving as expected.

My XAML for the right tab control is essentially the same as for the left one with the exception of the "workspace" collection (left uses "workspaces" right uses "chartspaces" so I'm at a loss as to what might be wrong or how to diagnose this problem further.

The datatemplate looks like:

    <DataTemplate x:Shared="False"  DataType="{x:Type vm:ModelOptionChainViewModel}">
    <vw:DDDRiskChart />
</DataTemplate> 

In further experimenting, I put the tab items on the left side instead and found the same problem, but if I selected a non-Dynamic Data Display tab item, then one of the DDD charts, they display correctly. So, before I selected a DDD chart I had to display a non-DDD chart, then the DDD chart for it to display.

So, moving the tab items back to the right tab control, I put the two DDD charts in, then put in a non-DDD chart (a DataVisualization chart). I found the same behavior...if I selected the non-DDD chart and then the DDD chart, it displays correctly. If I just alternate between the two DDD charts, they will both display the same graph! Is that not weird?

John Blacker
  • 129
  • 1
  • 10
  • Post your code here. – Rohit Vats Oct 02 '14 at 19:00
  • Keep in mind that a ViewModel is an application-specific concept that doesn't know about views (i.e. it's decoupled). *you* have to bind that ViewModel to your view manually/explicitly. We *can't* know if/how you're doing that; so provide details if you want someone to be able to help. – Peter Ritchie Oct 02 '14 at 19:03
  • How are you exposing your list of child View Models? In an ObservableCollection<> ? – Dave Mackersie Oct 02 '14 at 19:11
  • I've edited my original question to provide more information. Regarding further experimentation. But, the child viewmodels are in an observablecollection of type ChartspaceViewModel. The DataTemplate is what binds the viewmodel/View pair. Sample code above. – John Blacker Oct 02 '14 at 20:04
  • I'm beginning to wonder if this has something to do with the Dynamic Data Display charts? – John Blacker Oct 02 '14 at 20:18
  • WPF will re-use a template if possible instead of creating a new one. The only thing it will change is the `DataContext` behind your template. In your case, WPF recognizes that both tabs should be drawn using a `TabItem` containing your `DDDRiskChart` control, so doesn't bother creating a 2nd copy of the control. Instead, if just updates the `DataContext` behind the control to be whatever item is selected. – Rachel Oct 02 '14 at 20:21

1 Answers1

0

So, for any of you who care...the problem is actually associated with the TabControl behavior. Irv Krivyakov has an excellent Code Project with description (link: http://www.codeproject.com/Articles/460989/WPF-TabControl-Turning-Off-Tab-Virtualization ) that describes the problem AND provides a TabContent behavior class that eliminates the problem. The crux of the problem has to do with virtualization within TabControl. Rather than repeat his explanation, please see the above link. Also, see: DataTemplate x:Shared=false has no impact on the view especially the last post on that thread as it discusses TabControl and DataTemplate behaviors. My fix was: use Irv Krivyakov's TabContent behavior and my TabControl now looks like:

    <DataTemplate x:Key="ChartspacesTemplate">
<TabControl 
    Name="chartTab"
  IsSynchronizedWithCurrentItem="True" 
  ItemsSource="{Binding}"
  behaviours:TabContent.IsCached="True"     //  THIS IS THE FIX!!
  ItemTemplate="{StaticResource ChartTabItemTemplate}"
  Margin="4"
  />

Note: the one line change. Now when I switch tabs the correct data shows.

Community
  • 1
  • 1
John Blacker
  • 129
  • 1
  • 10