{"record":{"id":"d8523ff46bd53279","repo":"MahApps/MahApps.Metro","slug":"the-property-selecteditems-may-only-be-set-on-li","errorCode":null,"errorMessage":"The property 'SelectedItems' may only be set on ListBox, MultiSelector or MultiSelectionComboBox elements.","messagePattern":"The property 'SelectedItems' may only be set on ListBox, MultiSelector or MultiSelectionComboBox elements\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/MahApps.Metro/Controls/Helper/MultiSelectorHelper.cs","lineNumber":35,"sourceCode":"    /// Defines a helper class for SelectedItems binding on <see cref=\"ListBox\"/>, <see cref=\"MultiSelector\"/> or <see cref=\"MultiSelectionComboBox\"/> controls.\n    /// </summary>\n    public static class MultiSelectorHelper\n    {\n        public static readonly DependencyProperty SelectedItemsProperty\n            = DependencyProperty.RegisterAttached(\n                \"SelectedItems\",\n                typeof(IList),\n                typeof(MultiSelectorHelper),\n                new FrameworkPropertyMetadata(null, OnSelectedItemsChanged));\n\n        /// <summary>\n        /// Handles disposal and creation of old and new bindings\n        /// </summary>\n        private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)\n        {\n            if (d is not (ListBox or MultiSelector or MultiSelectionComboBox))\n            {\n                throw new ArgumentException(\"The property 'SelectedItems' may only be set on ListBox, MultiSelector or MultiSelectionComboBox elements.\");\n            }\n\n            if (e.OldValue != e.NewValue)\n            {\n                GetSelectedItemBinding(d)?.UnBind();\n\n                if (e.NewValue is IList newList)\n                {\n                    var multiSelectorBinding = new MultiSelectorBinding((Selector)d, newList);\n                    SetSelectedItemBinding(d, multiSelectorBinding);\n                    multiSelectorBinding.Bind();\n                }\n                else\n                {\n                    SetSelectedItemBinding(d, null);\n                }\n            }\n        }","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/MahApps/MahApps.Metro/blob/72099e310bac2d12ac98fd7560b69679252519f5/src/MahApps.Metro/Controls/Helper/MultiSelectorHelper.cs#L17-L53","documentation":"Thrown by MultiSelectorHelper.OnSelectedItemsChanged when the dependency object the SelectedItems attached property is set on is not a ListBox, MultiSelector, or MultiSelectionComboBox. The helper binds an IList to the selection of multi-selection controls only; attaching it to any other element type is unsupported and rejected at the change callback.","triggerScenarios":"Setting helper:MultiSelectorHelper.SelectedItems on a ComboBox, ItemsControl, TreeView, or any non-multi-selection Selector; applying the attached property to a custom control that is not derived from the supported base types.","commonSituations":"Trying to two-way bind SelectedItems on a plain ComboBox or ItemsControl where the framework has no SelectedItems property; copy-pasting the attached property from a ListBox onto an incompatible control.","solutions":["Use a ListBox, MultiSelector-derived control (DataGrid, ListView), or MultiSelectionComboBox as the target.","For single-selection controls (ComboBox), bind SelectedItem instead.","If you need SelectedItems on a custom control, derive it from ListBox/MultiSelector or implement MultiSelectionComboBox semantics.","Remove the MultiSelectorHelper.SelectedItems setter from incompatible elements."],"exampleFix":"<!-- before -->\n<ComboBox helper:MultiSelectorHelper.SelectedItems=\"{Binding Picks}\" /> <!-- throws -->\n\n<!-- after -->\n<ListBox helper:MultiSelectorHelper.SelectedItems=\"{Binding Picks}\" />","handlingStrategy":"type-guard","validationCode":"if (d is not (ListBox or MultiSelector or MultiSelectionComboBox))\n{\n    throw new ArgumentException(\n        $\"{d.GetType()} is not supported by MultiSelectorHelper.SelectedItems.\");\n}","typeGuard":"static bool SupportsMultiSelectorSelectedItems(DependencyObject d)\n    => d is ListBox or MultiSelector or MultiSelectionComboBox;","tryCatchPattern":null,"preventionTips":["Set MultiSelectorHelper.SelectedItems only on ListBox, MultiSelector, or MultiSelectionComboBox.","For single-selection controls, bind SelectedItem instead.","Derive custom multi-selection controls from the supported base types."],"tags":["wpf","attached-property","mahapps","multi-selector","type-guard"],"backgroundTag":null,"analyzedSha":"72099e310bac2d12ac98fd7560b69679252519f5","analyzedAt":"2026-08-13T20:19:34.419Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}