I'm trying to toggle a control between compact and expanded states based on the screen size. To do this I'm using visual states to change the control's template:
<UserControl>
<Grid>
<Grid.Resources>
<ControlTemplate x:Key="CompactMenuTemplate" TargetType="HeaderedItemsControl">
<Border>
<StackPanel>
<Image icon-menu.png" Cursor="Hand"/>
<Popup>
<Border Background="Blue">
<ItemsPresenter />
</Border>
</Popup>
</StackPanel>
</Border>
</ControlTemplate>
<ControlTemplate x:Key="ExpandedMenuTemplate" TargetType="HeaderedItemsControl">
<StackPanel Orientation="Horizontal" IsItemsHost="True" />
</ControlTemplate>
</Grid.Resources>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ScreenSizeGroups">
<VisualState x:Name="Wide" />
<VisualState x:Name="Small">
<Storyboard>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="linkItems"
Storyboard.TargetProperty="Template">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{StaticResource CompactMenuTemplate}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<HeaderedItemsControl x:Name="linkItems"
Template="{StaticResource ExpandedMenuTemplate}">
<Button Content="link #1" />
<Button Content="link #2" />
<Button Content="link #3" />
</HeaderedItemsControl>
</Grid>
</UserControl>
This results in an InvalidOperationException
when the visual state changes:
This Freezable cannot be frozen.
Is there some trick to make this work the way I want it to, or do I need to take a different approach?