【问题标题】:Change template of list item in WinRT app based on selection state根据选择状态更改 WinRT 应用中列表项的模板
【发布时间】:2013-06-24 17:09:01
【问题描述】:

我尝试根据是否选择项目来更改 WinRT 应用程序中 ListViewItem 的显示。在 WPF 中,我会使用样式触发器,但这在 WinRT 中不可用。所以在网上搜索了一段时间后,我想我必须使用可视化状态管理器。据我了解,ListView 有一个视觉状态组“SelectionStates”,其中包含我感兴趣的状态。所以我想出了以下 ItemContainerStyle,我将其应用于我的 ListView:

        <Style x:Key="itemContainerStyle" TargetType="SelectorItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="SelectorItem">
                    <Grid>
                        <TextBlock x:Name="textBlock" Text="{Binding}" Visibility="Visible" />
                        <TextBox x:Name="textBox" Text="{Binding Path=Name, Mode=TwoWay}" Visibility="Collapsed" />

                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected" />

                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="textBox" Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="textBlock" Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>

                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

正常项目按预期显示(所以我假设应用了我的样式),但选择的东西根本不起作用。为什么?我假设状态是自动触发的,不是吗?那么我该如何解决呢?

注意:我使用“SelectorItem”作为目标类型,因为我也想在网格视图上使用相同的样式。那是问题吗?将其更改为“ListViewItem”并没有解决我的问题......

提前致谢, 克里斯托夫

【问题讨论】:

    标签: windows xaml winrt-xaml


    【解决方案1】:

    您需要使用(UIElement.Visibility) 作为DiscreteObjectKeyFrameValue 属性的值。

    <VisualState x:Name="Selected">
        <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="textBox" Storyboard.TargetProperty="(UIElement.Visibility)">
                <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="textBlock" Storyboard.TargetProperty="(UIElement.Visibility)">
                <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </VisualState>
    

    【讨论】:

    • 感谢您的建议。我已经改变了,但我一点效果都没有......我感觉状态改变根本没有被触发,但我不知道如何调试它。
    • 好的,检查一下,我认为视觉状态不能正常工作。 stackoverflow.com/questions/17010582/…
    • 感谢您的帮助 - 我同时发现了问题:列表中的选择被禁用。
    • Hay @Christoph,我有同样的问题,如果列表中的选择被禁用,那么我可以在 ListviewItem 样式的 VisualState 管理器中使用 SelectionStates。由于 FocusStates 工作正常,但我需要 SelectionStates,因为当我们失去焦点时,VisualState 中的动画被反转,如何压倒这个 plz。
    【解决方案2】:

    我发现了问题:列表的SelectionMode 设置为None。因此,从未触发状态更改。

    此外,必须考虑多个选择状态,例如SelectedUnfocused。我发现在“样式和模板”上进行网络搜索对于在 MSDN 上找到合适的页面非常有帮助。

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 2018-06-07
      • 2012-09-18
      • 1970-01-01
      • 2010-10-24
      • 2012-08-20
      相关资源
      最近更新 更多