2

我正在尝试根据屏幕大小在紧凑状态和展开状态之间切换控件。为此,我使用视觉状态来更改控件的模板:

<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 不能被冻结。

是否有一些技巧可以按照我想要的方式进行这项工作,还是我需要采取不同的方法?

4

0 回答 0