通过可视状态更改控制模板会导致错误:"此Freezable无法冻结"

时间:2015-11-09 16:58:21

标签: wpf xaml visualstatemanager freezable

我试图在基于的情况下在紧凑状态和扩展状态之间切换控件 屏幕尺寸。为此,我使用视觉状态更改控件的模板:

<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>

当视觉状态发生变化时,这会产生InvalidOperationException

  

此Freezable无法冻结。

是否有一些技巧可以按照我想要的方式进行,或者我是否需要采取不同的方法?

0 个答案:

没有答案