简体   繁体   English

使用SelectionMode = Extended选择列表框

[英]Listbox IsSelected with SelectionMode=Extended

Sorry for the vague title, I couldn't come up with a good way to summarize what is happening. 对于模糊的标题感到抱歉,我无法想出一个总结正在发生的事情的好方法。

I have a bound WPF listbox: 我有一个绑定的WPF列表框:

<UserControl.Resources>
    <DataTemplate DataType="{x:Type local:MyBoundObject}">
        <TextBlock Text="{Binding Label}" />
    </DataTemplate>
</UserControl.Resources>

<ListBox ItemsSource="{Binding SomeSource}" SelectionMode="Extended">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsSelected Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

I want to operate on ONLY the selected items. 我想只对所选项目进行操作。 I do this by iterating through a list of all items and checking each object to see if it's IsSelected property is set. 我这样做是通过遍历所有项目的列表并检查每个对象来查看是否设置了IsSelected属性。

This works except for when I have many items in the list (enough so they are not all visible) and I press CTRL-A to select all items. 除了当我在列表中有很多项目(足够,所以它们不是全部可见)时,这是有效的,我按CTRL-A选择所有项目。 When I do this, all the visible items have their IsSelected property set to true, and all the rest are left false. 当我这样做时,所有可见项的IsSelected属性都设置为true,其余所有项都保留为false。 As soon as I scroll down, the other items come into view and their IsSelected properties are then set to true. 一旦我向下滚动,其他项目就会进入视图,然后它们的IsSelected属性将设置为true。

Is there any way to fix this behaviour so that every object's IsSelected property is set to true when I press CTRL-A? 有没有办法解决这种行为,以便当我按CTRL-A时,每个对象的IsSelected属性都设置为true?

Try set the 尝试设置

ScrollViewer.CanContentScroll="False"

on the ListBox, it should fix the ctrl+a problem. 在ListBox上,它应该修复ctrl +一个问题。

If you want get all selected items you can use SelectedItems property from ListBox. 如果要获取所有选定项,可以使用ListBox中的SelectedItems属性。 You don't need to add IsSelected property to your object. 您不需要将IsSelected属性添加到对象。

Check below example. 请查看以下示例。

XAML file: XAML文件:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <StackPanel Orientation="Horizontal">
        <Button Content="Selected items" Click="Button_Click" />
        <Button Content="Num of IsSelected" Click="Button_Click_1" />
    </StackPanel>

    <ListBox Name="lbData" SelectionMode="Extended" Grid.Row="1">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>

Code-behind file: 代码隐藏文件:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;

namespace ListBoxItems
{   
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<MyBoundObject> _source = new List<MyBoundObject>();
            for (int i = 0; i < 100000; i++)
            {
                _source.Add(new MyBoundObject { Label = "label " + i });
            }
            lbData.ItemsSource = _source;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(lbData.SelectedItems.Count.ToString());
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            int num = 0;
            foreach (MyBoundObject item in lbData.Items)
            {
                if (item.IsSelected) num++;
            }

            MessageBox.Show(num.ToString());
        }
    }

    public class MyBoundObject
    {
        public string Label { get; set; }
        public bool IsSelected { get; set; }
    }
}

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

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