编程笔记

C#-ListBox多选绑定

12/19/2024 10:18:22 PM
0

ListBox有一个依赖属性SelectedItems,但是这个属性是只读的,所以无法适用绑定,来自动获取多选项,如何通过绑定获取多选项,我们可以使用附加属性来实现。

定义一个附加属性

//ListBoxHelper.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ListBoxTest
{
    public class ListBoxHelper
    {
        public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached("SelectedItems",
            typeof(IList),
            typeof(ListBoxHelper),
            new FrameworkPropertyMetadata(default(IList),FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,OnSelectedItemsChanged));
        public static IList GetSelectedItems(DependencyObject obj)
        {
            return (IList)obj.GetValue(SelectedItemsProperty);
        }
        public static void SetSelectedItems(DependencyObject obj,IList value)
        {
            obj.SetValue(SelectedItemsProperty, value);
        }
        public static void OnSelectedItemsChanged(DependencyObject target,DependencyPropertyChangedEventArgs e)
        {
            var listBox = target as ListBox;
            if (listBox != null && (listBox.SelectionMode == SelectionMode.Multiple)) 
            {
                if (e.OldValue != null)
                {
                    listBox.SelectionChanged -= OnListBoxSelectionChanged;
                }
                IList collection=e.NewValue as IList;
                listBox.SelectedItems.Clear();
                if (collection != null)
                {
                    foreach(var item in collection)
                    {
                        listBox.SelectedItems.Add(item);
                    }
                    listBox.SelectionChanged += OnListBoxSelectionChanged;
                }
            }
        }
        public static void OnListBoxSelectionChanged(object sender,SelectionChangedEventArgs e)
        {
            IList dataSource = GetSelectedItems(sender as DependencyObject);
            foreach(var item in e.AddedItems)
            {
                dataSource.Add(item);
            }
            foreach(var item in e.RemovedItems)
            {
                dataSource.Remove(item);
            }
        }
    }
}

使用附加属性绑定多选项

将ListBox的选中模式设置为多选SelectionMode="Multiple",添加附加属性,并设置绑定项local:ListBoxHelper.SelectedItems="{Binding SelectedItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"。

<Window x:Class="ListBoxTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ListBoxTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel Orientation="Vertical" HorizontalAlignment="Left">
            <TextBlock Width="200" Height="30" Text="{Binding SelectedItemsText,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
            <ListBox SelectionMode="Multiple" ItemsSource="{Binding Items}" local:ListBoxHelper.SelectedItems="{Binding SelectedItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  Width="200" Height="100"></ListBox>
        </StackPanel>
    </Grid>
</Window>

效果

完整参考代码

//mainwindow.xaml
<Window x:Class="ListBoxTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ListBoxTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel Orientation="Vertical" HorizontalAlignment="Left">
            <TextBlock Width="200" Height="30" Text="{Binding SelectedItemsText,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
            <ListBox SelectionMode="Multiple" ItemsSource="{Binding Items}" local:ListBoxHelper.SelectedItems="{Binding SelectedItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  Width="200" Height="100"></ListBox>
        </StackPanel>
    </Grid>
</Window>
//mainwindow.xaml.cs
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ListBoxTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainWindowViewModel();
        }
    }
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        public MainWindowViewModel()
        {
            Items.Add("123");
            Items.Add("456");
            Items.Add("789");

        }
        private ObservableCollection<string> m_Items = new ObservableCollection<string>();
        public ObservableCollection<string> Items
        {
            get
            {
                return m_Items;
            }
            set
            {
                m_Items = value;
                OnPropertyChanged(nameof(Items));
            }
        }
        private ObservableCollection<string> m_SelectedItems = new ObservableCollection<string>();
        public ObservableCollection <string> SelectedItems
        {
            get
            {
                if (m_SelectedItems != null)
                {
                    m_SelectedItems.CollectionChanged += OnSelectedItemsCollectionChanged;
                }
                return m_SelectedItems;
            }
            set
            {
                m_SelectedItems = value;
                OnPropertyChanged(nameof(SelectedItems));
            }
        }
        private string m_SelectedItemsText;
        public string SelectedItemsText
        {
            get
            {
                return m_SelectedItemsText;
            }
            set
            {
                m_SelectedItemsText= value;
                OnPropertyChanged(nameof(SelectedItemsText));
            }
        }
        private void OnSelectedItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in SelectedItems)
            {
                if (sb.Length > 0) sb.Append(", ");
                sb.Append(item);
            }
            SelectedItemsText = sb.ToString();
        }
        public event PropertyChangedEventHandler? PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ListBoxTest
{
    public class ListBoxHelper
    {
        public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached("SelectedItems",
            typeof(IList),
            typeof(ListBoxHelper),
            new FrameworkPropertyMetadata(default(IList),FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,OnSelectedItemsChanged));
        public static IList GetSelectedItems(DependencyObject obj)
        {
            return (IList)obj.GetValue(SelectedItemsProperty);
        }
        public static void SetSelectedItems(DependencyObject obj,IList value)
        {
            obj.SetValue(SelectedItemsProperty, value);
        }
        public static void OnSelectedItemsChanged(DependencyObject target,DependencyPropertyChangedEventArgs e)
        {
            var listBox = target as ListBox;
            if (listBox != null && (listBox.SelectionMode == SelectionMode.Multiple)) 
            {
                if (e.OldValue != null)
                {
                    listBox.SelectionChanged -= OnListBoxSelectionChanged;
                }
                IList collection=e.NewValue as IList;
                listBox.SelectedItems.Clear();
                if (collection != null)
                {
                    foreach(var item in collection)
                    {
                        listBox.SelectedItems.Add(item);
                    }
                    listBox.SelectionChanged += OnListBoxSelectionChanged;
                }
            }
        }
        public static void OnListBoxSelectionChanged(object sender,SelectionChangedEventArgs e)
        {
            IList dataSource = GetSelectedItems(sender as DependencyObject);
            foreach(var item in e.AddedItems)
            {
                dataSource.Add(item);
            }
            foreach(var item in e.RemovedItems)
            {
                dataSource.Remove(item);
            }
        }
    }
}

友情链接

Copyright @2021-2025 关于