WPF : Multiple views, one DataContext

Posted by zapho on Stack Overflow See other posts from Stack Overflow or by zapho
Published on 2010-05-07T16:46:49Z Indexed on 2010/05/07 16:48 UTC
Read the original article Hit count: 330

Filed under:
|
|

Hi,

I'm working on a WPF application which must handle multiple screens (two at this this time).

One view can be opened on several screens and user actions must be reflected consistently on all screens.

To achieve this, for a given type of view, a single DataContext is instantiated. Then, when a view is displayed on a screen, the unique DataContext is attached to it. So, one DataContext, several views (same type of view/xaml).

So far so good. It works quite well in most cases.

I do have a problem with a specific view which relies on ItemsControl. These ItemsControl are used to display UIElements dynamically build in the ViewModel/DataContext (C# code). These UIElements are mostly Path objects. Example :

<ItemsControl ItemsSource="{Binding WindVectors}">
   <ItemsControl.Template>
     <ControlTemplate TargetType="{x:Type ItemsControl}">
       <Canvas IsItemsHost="True" />
     </ControlTemplate>
   </ItemsControl.Template>
 </ItemsControl>

Here, WindVectors is a ObservableCollection<UIElement>.

When the view is opened the first time, everything is fine. The problem is that when the view is opened one another screen, all ItemsControl are removed from the first screen and displayed one the second screen. Other WPF components (TextBlock for instance) on this view react normally and are displayed on both screens.

Any help would be greatly appreciated.

Thanks.

Fabrice

© Stack Overflow or respective owner

Related posts about wpf

Related posts about datacontext