绑定ListBox&的ObservableCollection

时间:2018-06-04 13:29:41

标签: c# wpf data-binding

我希望将我的ListBox与WPF应用程序中的ObservableCollection绑定,以获取用户对多个项目的选择,并尝试使用SelectedItemsProperty,但它不起作用。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        FamilyListBox.SetBinding(ListBox.ItemsSourceProperty, new Binding("Families"));
        FamilyListBox.SelectionMode = SelectionMode.Multiple;
        FamilyListBox.SetBinding(ListBox.SelectedItemsProperty, new binding("SelectedFamilies"));
   }

这是我的访问者:

   private ObservableCollection<string> _families = new ObservableCollection<string>();
   public ObservableCollection<string> SelectedFamilies
   {
        get { return _selectedFamilies; }
        set
        {
            if (value == _selectedFamilies)
                return;
            _selectedFamilies = value;
            NotifyPropertyChanged("SelectedFamilies");
        }
    }

我最终得到了这个错误:

An exception of type 'System.ArgumentException' occurred in PresentationFramework.dll but was not handled in user code

0 个答案:

没有答案