gpt4 book ai didi

c# - 根据自定义属性 UWP 禁用某些 ListViewItem

转载 作者:行者123 更新时间:2023-11-30 16:02:53 26 4
gpt4 key购买 nike

我有一个 ListView包含多种类型的自定义 UserControls .

项目要求其中一些必须是不可点击的,所以我想禁用它们,但是JUST THEM

这些项目将根据自定义属性的值启用/禁用。

我尝试设置 ListViewItem.IsEnabled属性(property)false ,但它没有用,我发现的其他解决方案对我来说毫无意义......

我放了一段代码示例:

XAML

<ListView x:Name="homeLW"
Margin="0,5,0,0"
ItemClick="homeLW_ItemClick"
IsItemClickEnabled="True"
HorizontalAlignment="Center"
ItemsSource="{Binding Source}">

在哪里SourceObservableCollection<UserControl> .

问题是我无法获取 ListView 的项目作为ListViewItems , 但作为 UserControl类型:。执行此操作时:

foreach(ListViewItem lwI in homeLW.Items)
{
//CODE
}

我得到:

System.InvalidCastException: Unable to cast object of type UserControl.Type to type Windows.UI.Xaml.Controls.ListViewItem.

有人知道我该怎么做吗?

提前致谢:)

最佳答案

foreach(var lwI in homeLW.Items)
{
ListViewItem item =(ListViewItem)homeLW.ContainerFromItem(lwI);
item.IsEnabled = false;
}

加载时,由于虚拟化,所有 ListViewItems 都不会加载。因此,当您尝试从项目中获取容器时,您会得到 Null。解决方法是关闭虚拟化。但它会产生性能影响。既然你确认它不会超过 20 个项目,我将继续添加代码

<ListView>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>

关于c# - 根据自定义属性 UWP 禁用某些 ListViewItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37132267/

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