wpf 中的多选列表框

发布于 2024-09-28 14:04:01 字数 90 浏览 1 评论 0原文

我如何在单击列表框中选择五个项目? 如果我单击任何项​​目,我只想从所选索引中+2 和-2。所以我的单击需要在列表视图中选择五个项目。 我正在使用 C#(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. so my single click need to select five items in the listview.
Am using C#(WPF).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不吻晚风 2024-10-05 14:04:01

我不确定你到底想做什么,但尝试一下。 =)

看一下ListBox 的Click 事件。您可以在那里做任何事情,包括选择您选择的五个项目。您可以这样做(未经测试,但给您一个想法):

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]);
}

另一件事是将 SelectionMode 设置为 Multiple 或 Extended。这会导致您正在寻找的行为吗?

I am not sure what you want to do exactly, but trying. =)

Have a look at the Click event of the ListBox. 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. Does this result in the behaviour you are looking for?

梨涡少年 2024-10-05 14:04:01

查看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]);
        }
    }
}

此代码的一个问题是您仍然可以使用 ctrl 来选择另一个项目,这样它会使选定的项目计数增加

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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文