WPF. ListBox. How to limit maximum of selected items to 2?

一世执手 提交于 2019-12-07 12:51:53

问题


I need to limit the number of selected items in a ListBox to 2. How to do this? Is it possible to avoid the usage of events and do this in XAML?


回答1:


There's no built-in way to do this as far as I'm aware, so you're going to have to write some code.

You could set your ListBox as multi-select by setting SelectionMode to Multiple or Extended (See here for the difference) and attach onto the SelectionChanged event. When the event is raised, modify the selection how you see fit (how you do it would depend on how you wanted it to work... if they select a third one, does it get removed right away? or does the first one get removed - effectively FIFO or LIFO removal).

Edit:

My bad, I had linked to the wrong article.




回答2:


I made a behavior of this where I can bind the number of elements I want to have selected to a dependencyproperty.

It's used like this, attached to a ListView

<i:Interaction.Behaviors>
    <behaviors:LimitSelectionBehavior Limit="2" />
</i:Interaction.Behaviors>

And here is the Behavior class. The Remove could be modified to unselect down to the Limit, now it just unselects all newly added.

public class LimitSelectionBehavior : Behavior<ListView>
{
    public static readonly DependencyProperty LimitProperty;

    static LimitSelectionBehavior()
    {
        LimitProperty = DependencyProperty.Register("Limit", typeof(int), typeof(LimitSelectionBehavior), new PropertyMetadata(default(int)));
    }

    public int Limit
    {
        get { return (int) GetValue(LimitProperty); }
        set { SetValue(LimitProperty, value); }
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.SelectionChanged += OnSelectionChanged;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.SelectionChanged -= OnSelectionChanged;
    }

    private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (AssociatedObject.SelectedItems.Count <= Limit)
            return;

        foreach (var added in e.AddedItems)
        {
            AssociatedObject.SelectedItems.Remove(added);
        }
    }
}



回答3:


Following is an example of how to restrict selection to two items only

For the ListView defined in the following XAML

  <ListView x:Name="MyListView" ItemsSource="{Binding Path=ParentSection.MyListItems}" BorderThickness="0"
                SelectionMode="Multiple" ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
                mvvm:FrameworkElementBehaviors.IgnoreMouseWheel="True" 
                SelectionChanged="MyListView_SelectionChanged">
    <ListView.View>
    <!--Your list view content here -->
    </ListView.View>
  </ListView>

The event would be something like following

    public void MyListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (this.MyListView.SelectedItems.Count > 2)
        {
            this.MyListView.SelectedItems.RemoveAt(0);
        }
    }


来源:https://stackoverflow.com/questions/6050886/wpf-listbox-how-to-limit-maximum-of-selected-items-to-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!