gpt4 book ai didi

c# - Windows 应用商店应用程序 : How to make ListView with expandable/enlargeable ListItems?

转载 作者:太空狗 更新时间:2023-10-30 01:05:17 27 4
gpt4 key购买 nike

我在 C# Windows 应用商店应用程序中有一个包含项目的 ListView (这是你对这些的称呼吗?我听说它们不再被称为 Metro Apps)。

类似于ExpandableListView在 Android 中,我希望能够点击列表项(不是按钮)来展开该列表项,点击展开的列表项来折叠它,如果你点击另一个列表项,当前展开的列表项将折叠,另一个将展开。

在我的特殊情况下,我有一个 DataTemplate 用于列表项的扩展 View 和非扩展 View 。我已经看到 Android 的 ExpandableListView 可以使用附加信息扩展列表项(WPF 中的 Expander 做类似的事情),而不是用更大的项目替换它 ,但是在 Windows Store Apps 中是否有一个通用的解决方案?如果不是,最接近的等价物是什么?

如下图,我想知道是否有一个组件可以这样扩展列表项,如果没有,我有哪些替代方案: Drawing of Expandable-ListItem in Windows Store App

最佳答案

我最终找到了一个可行但看起来不太花哨的解决方案。当您单击项目但没有动画时,它会切换 DataTemplate:它会立即切换。

这里是重要的代码部分:

XAML

<Page.Resources>
<DataTemplate x:Key="dtSmall">
<!--Component template for the un-expanded listitems-->
</DataTemplate>
<DataTemplate x:Key="dtEnlarged">
<!--Component template for the expanded listitems-->
</DataTemplate>
</Page.Resources>
<Grid>
<ListView x:Name="lvEnlargeable"
IsItemClickEnabled="True"
ItemTemplate="{StaticResource dtSmall}"
ItemsSource="{Binding ...}"
SelectionChanged="LVEnlargeable_SelectionChanged"
ItemClick="LVEnlargeable_ItemClick"/>
</Grid>

XAML.CS

public sealed partial class MainPage : Page
{
private DataTemplate dtSmall;
private DataTemplate dtEnlarged;

public MainPage()
{
this.InitializeComponent();
dtSmall = (DataTemplate)Resources["dtSmall"];
dtEnlarged = (DataTemplate)Resources["dtEnlarged"];
}

// A selected item is treated as an expanded/enlarged item
private void LVEnlargeable_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
/* First we set all the items that has been deselected
to be collapsed, aka. using the dtSmall DataTemplate.
We expect 0 or 1 item to have been deselected
but handle all cases easily with a foreach loop.
*/
foreach (var item in e.RemovedItems)
{
// Set the DataTemplate of the deselected ListViewItems
((ListViewItem)(sender as ListView).ContainerFromItem(item)).ContentTemplate = dtSmall;
}

/* Then we set all the items that has been selected
to be expanded.
We should probably throw an Exception if more than 1 was found,
because it's unwanted behavior, but we'll ignore that for now.
*/
foreach (var item in e.AddedItems)
{
((ListViewItem)(sender as ListView).ContainerFromItem(e.AddedItems[0])).ContentTemplate = dtEnlarged;
}
}

/* We need click events because SelectionChanged-events
cannot detect clicks on an already selected item */
private void LVEnlargeable_ItemClick(object sender, ItemClickEventArgs e)
{
ListView lv = (sender as ListView);

/* Having set the IsItemClickEnabled property on the ListView to True
we have to handle selection events manually.
If nothing is selected when this click occurs, then select this item*/
if (lv.SelectedItem == null)
{
lv.SelectedItem = e.ClickedItem;
}
else
{
// Clicking on an expanded/selected/enlarged item will deselect it
if (lv.SelectedItem.Equals(e.ClickedItem))
{
lv.SelectedItem = null;
}
else
{ /* If it's not a selected item, then select it
(and let SelectionChanged unselect the already selected item) */
lv.SelectedItem = e.ClickedItem;
}
}
}
}

我还没有测试这个独立的代码是否足以解决这个问题,但我希望是这样,并且这个代码至少包含了关键点。已经很晚了,我只是想为有好奇心的人发布一些东西。如果这对您不起作用,请就此问题发表评论,我会确保添加缺失的部分。

我还搞砸了 ListViewItemStyleContainer 的 ListViewItemPresenter 以获得更好的选择效果等,但我认为最好保持简短。如果您也觉得这很有趣,那么也请随时发表评论,我会尝试将其包括在内。

关于c# - Windows 应用商店应用程序 : How to make ListView with expandable/enlargeable ListItems?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19379727/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com