DataContext lost in a ModelVisual3D??? Is it a bug?

Hi all.

At the end of this post I added 2 pieces of code. Basically, the second one is the same as the first one but wrapping the _itemContent with a Viewport3D so that 3D animations are possible on the list items.

In both cases the _itemsContainer.ItemsSource is set in the code behind in the Loaded event handler of the Window.

However, the 2nd code doesnt work and after many experiments I detected that the DataContext is lost. My question is: is this supposed to happen For some reason the VisualBrush "blocks" the cascading of the DataContext Is there another way to create a 3D ItemTemplate

The Templating2 sample here http://msdn.microsoft.com/msdntv/episode.aspx xml=episodes/en/20060202WPFKM/manifest.xml works ok using an ImageBrush and binding the ImageSource="{Binding XPath=@image}", but switching it by a TextBlock within a VisualBrush has the same effect: DataContext is null thus no data is actually shown.

Thanks a lot in advance for any help,

RODOLFO

These are the 2 pieces of XAML code:
<code>
<!-- First piece -->
<ItemsControl x:Name="_itemsContainer" >
<ItemsControl.Panel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.Panel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="_itemContent" Background="LightGreen" Width="100" Height="25" Text="{Binding Path=Id}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- End of first piece-->

<!--Second piece -->
<ItemsControl x:Name="_itemsContainer" >
<ItemsControl.Panel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.Panel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Viewport3D x:Name="Foo" Width="100" Height="100">
<Viewport3D.Camera>
<PerspectiveCamera Position="0,0,-6" LookDirection="0,0,1" FieldOfView="18"/>
</Viewport3D.Camera>

<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<GeometryModel3D x:Name="OB_Cube">
<GeometryModel3D.Material>
<DiffuseMaterial x:Name="MA_Material">
<DiffuseMaterial.Brush>
<VisualBrush AutoLayoutContent="True">
<VisualBrush.Visual>
<TextBlock x:Name="_itemContent" Background="LightGreen" Width="100" Height="25" Text="{Binding Path=Id}"/>
</VisualBrush.Visual>
</VisualBrush>
</DiffuseMaterial.Brush>
</DiffuseMaterial>
</GeometryModel3D.Material>
</GeometryModel3D>
</Model3DGroup>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!--End of second piece -->
</code>



Answer this question

DataContext lost in a ModelVisual3D??? Is it a bug?

  • Rainman808

    This is indeed a bug and has been fixed in the upcoming release.

    Generally speaking, the issue is that inheritance does not propagate from the VisualBrush to the FrameworkElement it contains causing the nested element not to be able to find its DataContext.

    A possible work around is to manually set the DataContext on the TextBlock from code.

    Thanks,
    - Daniel [MSFT]

    ----
    All postings are provided "As Is" with no warranties, and confers no rights.


  • Shizgnome

    Thanks a lot for your help Daniel. It's nice to hear this is a FIXED bug.

    However, the workaround you suggested leads to another question: how can I bind to "the item being painted" so that I can do something like the following:

    <TextBlock x:Name="_itemContent" Background="LightGreen" Width="100" Height="25" Text="{Binding Source=TheItemBeingPainted, Path=Id}"/>

    Thanks a million again.

    RODOLFO


  • JackyCheng

    Thanks a lot for your time Daniel. I installed Beta2 and I'm very glad to see it's fixed. The next thing would be to have an interactive VisualBrush... do you know if that's comming any time soon

    Thanks again,

    RODOLFO

  • DataContext lost in a ModelVisual3D??? Is it a bug?