简体   繁体   English

WPF中的多选择列表框

[英]mulit select listbox in wpf

how i can select five items in the single click on the list box?? 我如何在列表框中单击即可选择五个项目? if i click any item, i just want to +2 and -2 from the selected index. 如果我单击任何项​​目,我只想从所选索引中将+2和-2设置为。 so my single click need to select five items in the listview. 因此,我只需单击一下即可在列表视图中选择五个项目。 Am using C#(WPF). 我正在使用C#(WPF)。

I am not sure what you want to do exactly, but trying. 我不确定您要确切地做什么,但可以尝试。 =) =)

Have a look at the Click event of the ListBox. 看一下ListBox的Click事件。 You can do anything in there, including selecting five items of your choice. 您可以在其中执行任何操作,包括选择五个项。 You can do it like this (untested, but gives you an idea): 您可以这样做(未经测试,但可以给您一个想法):

int sindex = listBox1.SelectedIndex;
listBox1.SelectedItems.Clear();
for(int i = Math.Max(sindex - 2, 0); i < Math.Min(sindex + 2, listBox1.Items.Count()), i++)
{
    listBox1.SelectedItems.Add(listBox1.Items[i]);
}

Another thing would be setting the SelectionMode to Multiple or Extended. 另一件事是将SelectionMode设置为Multiple或Extended。 Does this result in the behaviour you are looking for? 这是否会导致您正在寻找的行为?

have a look at selectionchanged event, and get the index of the selected item and make it +2 and -2 i tried it like this and it works: 看一看selectionchanged事件,并获取所选项目的索引,并将其设置为+2和-2,我这样尝试过,它的工作原理是:

void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    int idx = list.SelectedIndex;
    int startIdx = idx - 2;
    int endIdx = idx + 2;
    if (startIdx < 0)
    {
        startIdx = 0;
    }
    if (endIdx >= list.Items.Count)
    {
        endIdx = list.Items.Count-1;
    }

    for (int i = startIdx; i <= endIdx; i++)
    {
        if (i != idx)
        {
            list.SelectedItems.Add(list.Items[i]);
        }
    }
}

one problem with this code is you can still use ctrl to select another item so it makes the selecteditems count increased 此代码的一个问题是,您仍然可以使用ctrl来选择另一个项目,从而使selecteditems计数增加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM