In WinUI 3 In ListView How to make single item selected and other all item unselected and unclickable?

huangapple go评论77阅读模式
英文:

In WinUI 3 In ListView How to make single item selected and other all item unselected and unclickable?

问题

在WinUI 3应用程序中,我想要在ListView中选择一个项目,并且它应该接受按键事件,而其他所有项目都应取消选择并禁用或不可点击。这是我试图实现此功能的代码:

internal static void MyListView(ListView view)
{
    view.SelectionMode = ListViewSelectionMode.None; // 尝试设置选择模式为'none'
    foreach (var i in view.Items)
    {
        ListViewItem item = (ListViewItem)i; 
    }
}

在这里,MyListView是在一个独立的类中调用的方法,在那里将ListView作为参数传递。

在UI中,我想要显示如下。

从下面的图像中了解问题。
In WinUI 3 In ListView How to make single item selected and other all item unselected and unclickable?

更新:

  • 在我的应用程序中,有一个显示项目列表的情景。

  • 在其中,从第一个到倒数第二个项目应该被禁用并且不可点击。

  • 最后一个项目应该已经被选择,并且我已经定义了一个按键事件。当我们按下空格键时,该事件触发,并且最后一个项目的颜色会改变。

  • 但是,如果我点击外面的其他禁用项目,那么已经选择的最后一个项目不会接受任何按键事件。

  • 这是因为禁用项目。在WinUI-3中,如果ListView项目被禁用,如果我们通过鼠标单击它,那么它不会接受我已经定义的任何按键事件(最后一个项目不会接受事件)。

  • 因此,我想要的功能是,在项目列表中,只有最后一个项目应该是可点击的,并且已经被点击,并且它接受按键事件。如果单击其他禁用的项目,它也应该接受按键事件。

In WinUI 3 In ListView How to make single item selected and other all item unselected and unclickable?

英文:

In WinUI 3 application in ListView I want one item selected and it should accept key event and other all item should be unselected and unclickable or disable.
This is the code where I am trying to do this functionality,

internal static void MyListView(ListView view)
{
    view.SelectionMode = ListViewSelectionMode.None; // tried to selection mode 'none'
    foreach (var i in view.Items)
    {
        ListViewItem item = (ListViewItem)i; 
    }
}

here MyListView is method calling in separate class, there passing List view as a parameter.
I tried this using setting selection mode 'none' but for perticular index item How to make selectable.

In UI I want to display Like This.

understand problem from below image.
In WinUI 3 In ListView How to make single item selected and other all item unselected and unclickable?

Updated:

  • In my application having scenario list of items display.

  • In that first to second last should be disabled and unclickable.

  • Last item is should be already selected and in that I am already defined a key event. when we press space key that event hit and that last item color is changing.

  • But if I click on the outside means other disable item my last item which is already selected it not accept any key event.

  • This is happening because of disabling items. In WinUi-3 when ListView Item are disabled and if we click on that through mouse then it not accept any key event which I defined already (Last Item not accept event).

  • So, I want functionality where List of items in that only last item should clickable and already click and It accept key event. if we click other disable item then also it should accept key event.

In WinUI 3 In ListView How to make single item selected and other all item unselected and unclickable?

答案1

得分: 2

你可以这样访问每个ListViewItem

// 在这种情况下,“view”是你的ListView控件。
foreach (object? item in view.Items)
{
    if (view.ContainerFromItem(item) is ListViewItem listViewItem)
    {
        listViewItem.IsEnabled = listViewItem.IsSelected;
    }
}

为了回答你在评论中提出的额外问题,你可以通过编程方式选择并订阅最后一个项目的键事件,然后像这样禁用其他项目:

// 选择最后一个项目。
int lastItemIndex = view.Items.Count - 1;

if (view.ContainerFromIndex(lastItemIndex) is ListViewItem lastListViewItem)
{
    lastListViewItem.IsSelected = true;
    lastListViewItem.KeyDown -= ListViewItem_KeyDown;
    lastListViewItem.KeyDown += ListViewItem_KeyDown;
}

// 禁用所有未选中的项目。
foreach (object? item in ListViewControl.Items)
{
    if (view.ContainerFromItem(item) is ListViewItem listViewItem)
    {
        listViewItem.IsEnabled = listViewItem.IsSelected;
    }
}
private void ListViewItem_KeyDown(object sender, KeyRoutedEventArgs e)
{
}
英文:

You can access each ListViewItem like this:

// In this case "view" is your ListView control.
foreach (object? item in view.Items)
{
    if (view.ContainerFromItem(item) is ListViewItem listViewItem)
    {
        listViewItem.IsEnabled = listViewItem.IsSelected;
    }
}

To answer your additional question from the comments, you can programmatically select and subscribe the last item to key events, then disable the other items like this:

// Select the last item.
int lastItemIndex = view.Items.Count - 1;

if (view.ContainerFromIndex(lastItemIndex) is ListViewItem lastListViewItem)
{
    lastListViewItem.IsSelected = true;
    lastListViewItem.KeyDown -= ListViewItem_KeyDown;
    lastListViewItem.KeyDown += ListViewItem_KeyDown;
}

// Disable all not-selected items.
foreach (object? item in ListViewControl.Items)
{
    if (view.ContainerFromItem(item) is ListViewItem listViewItem)
    {
        listViewItem.IsEnabled = listViewItem.IsSelected;
    }
}
private void ListViewItem_KeyDown(object sender, KeyRoutedEventArgs e)
{
}

huangapple
  • 本文由 发表于 2023年7月10日 14:03:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定