Windows Phone 8.1 ListView 项可见性取决于某些条件

Windows Phone 8.1 ListView item visibility depending on some condition

我正在使用 C#/XAML 开发 Windows Phone 8.1 应用程序。应用程序的主视图包含 Windows.UI.Xaml.Controls.ListView class 填充的项目(某些搜索查询的结果)。

当用户点击一个项目时,该项目的附加信息面板 (ExtendedInfoPanel) 应该出现,但仅针对 this 项目(即一次只有 一个项目 应该公开该附加信息)。

我尝试通过编辑默认 ItemContainerStyle 的副本来自定义 <VisualStateGroup x:Name="SelectionStates">,但没有成功。 这是我的代码(只有相关部分):

<Style x:Key="ListViewItemStyle1" TargetType="ListViewItem">
    <VisualStateGroup x:Name="SelectionStates">
        <VisualState x:Name="Unselected"/>
        <VisualState x:Name="SelectedUnfocused">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ExtendedInfoPanel">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</Style>
<ListView
    x:Name="myList"
    Margin="0,0,0,0"
    IsItemClickEnabled="False"
    ItemsSource="{Binding Source={StaticResource myDataSource}}"
    SelectionMode="Single"
    ItemTemplate="{StaticResource mySongDataTemplate}"
    ItemContainerStyle="{StaticResource ListViewItemStyle1}">
</ListView>

运行 上面的代码片段以 Windows.UI.Xaml.UnhandledExceptionEventArgs 消息在应用程序中抛出异常结束:

Cannot resolve TargetName ExtendedInfoPanel.

任何人都可以给我一些提示,我怎样才能实现所需的行为?

当您在数据模板中命名元素时,您无法在该数据模板之外通过名称访问它们。 Jerry Nixon 解释了这样做的原因和访问该元素的方法 here

简而言之,您可以导航可视化树并找到您的 ExtendedInfoPanel 元素并更改它的可见性。但是,在您的问题中,您说您只希望一个项目显示它的详细信息。这意味着您应该在可视化树中搜索所有其他 ExtendedInfoPanel 元素并隐藏它们。这听起来不是很有效。

我的方法更像 mvvm。在您的模型中,为将与 ExtendedInfoPanel 关联的 class 提供布尔值 IsVisible 属性,并将其绑定到元素的可见性。

然后在列表的点击事件中将点击项目的数据上下文转换为您的模型 class 并更改点击元素和集合中所有其他元素的 IsVisible 属性 . 提示:您将需要一个 ObservableCollection,以便将更改传播到 UI。或者,您将需要实施 INotifyPropertyChanged。