根据自定义属性 UWP 禁用某些 ListViewItem [英] Disable certain ListViewItem depending on custom property UWP

查看:22
本文介绍了根据自定义属性 UWP 禁用某些 ListViewItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a ListView that contains several types of custom UserControls.

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

The project requires that some of them must be non-clickable, so I would like to disable them, but JUST THEM.

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

Those items will be enabled/disabled depending on the value of a custom property.

我尝试将 ListViewItem.IsEnabled 属性设置为 false,但它不起作用,而且我发现的其他解决方案毫无意义对我...

I've tried to set the ListViewItem.IsEnabled property to false, but it ain't worked, and the other solutions I've found around make no sense to me...

我放了一个代码示例:

XAML

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

其中 SourceObservableCollection.

问题是我无法将 ListView 的项目作为 ListViewItems,而是作为 UserControl 类型:.执行此操作时:

The problem is that I can't get the items of the ListView as ListViewItems, but as the UserControl type:. When executing this:

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

我得到:

System.InvalidCastException:无法转换类型的对象UserControl.Type 以键入 Windows.UI.Xaml.Controls.ListViewItem.

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

有人知道我是怎么做到的吗?

Anyone know how could I make it?

提前致谢:)

推荐答案

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

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

When on load all ListViewItems wont be loaded because of Virtualization. So you get Null when try to get container from item. Workaround would be switching off the virtualization. But it will have performance effects. Since you confirmed that it wont be having more than 20 items,I ll go ahead and add the code

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

这篇关于根据自定义属性 UWP 禁用某些 ListViewItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆