基于 VisualState 禁用 Flyout

Disable Flyout based on VisualState

我尝试 add/remove 基于 VisualStateManagerMenuFlyout

我有两个状态 TouchNoTouch。我想在名为 rowGrid 的网格上设置 Flyout。我尝试了不同版本的 VisualStates。 例如:

<VisualStateGroup x:Name="TouchGroup">
    <VisualState x:Name="Touch">
        <VisualState.Setters>
            <Setter Target="rowGrid.ContextFlyout">
                <Setter.Value>
                    <MenuFlyout >
                        <MenuFlyoutItem Text="Play" />
                    </MenuFlyout>
                </Setter.Value>
            </Setter>
        </VisualState.Setters>
    </VisualState>
    <VisualState x:Name="NoTouch">
        <VisualState.Setters>
            <Setter Target="rowGrid.ContextFlyout" Value="{x:Null}" />
        </VisualState.Setters>
    </VisualState>
</VisualStateGroup>

然而,一旦使用 StateManager.GoToState(...) 方法激活 NoTouch 状态,这将抛出 COMException (E_FAIL)。另一个状态将设置 Flyout,但没有任何内容,仅提供一个空菜单。

网格本身位于 ListView

DataTemplate

我错过了什么?

Disable Flyout based on VisualState

Flyout is lazy load control, it could not be initialized with VisualState, it need to be initialized at previous in the xaml. If you want to disable Flyout based on VisualState, you could change the MenuFlyoutPresenter样式隐藏了Flyout,具体请参考以下代码

<Grid Background="Transparent" x:Name="rowGrid" >
    <Grid.ContextFlyout >
        <MenuFlyout x:Name="GridMenuFlyout" >
            <MenuFlyoutItem Text="Reset"/>
            <MenuFlyoutSeparator/>
            <ToggleMenuFlyoutItem Text="Repeat"/>
            <ToggleMenuFlyoutItem Text="Shuffle"/>
        </MenuFlyout>
    </Grid.ContextFlyout>

    <Grid.Resources>

        <Style TargetType="MenuFlyoutPresenter" x:Key="MenuFlyoutPresenterShowItem">
            <Setter Property="Visibility" Value="Visible"/>
        </Style>
        <Style TargetType="MenuFlyoutPresenter" x:Key="MenuFlyoutPresenterHideItem">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>

    </Grid.Resources>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="TouchGroup">
            <VisualState x:Name="Touch">
                <VisualState.Setters>
                    <Setter Target="GridMenuFlyout.MenuFlyoutPresenterStyle" Value="{ThemeResource MenuFlyoutPresenterShowItem}"/>
                </VisualState.Setters>
            </VisualState>

            <VisualState x:Name="NoTouch">
                <VisualState.Setters>
                    <Setter Target="GridMenuFlyout.MenuFlyoutPresenterStyle" Value="{ThemeResource MenuFlyoutPresenterHideItem}"/>
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
</Grid>