private void RemoveSelected()
        {
            var index = SelectedTemplatesListBox.SelectedIndex;

            var selectedItems = SelectedTemplatesListBox.SelectedItems.OfType <ListBoxItem>().Select(listBoxItem => listBoxItem.Tag).OfType <TemplateHeader>().Select(templateHeader => templateHeader.TemplateId).ToList();

            foreach (var selectedItem in selectedItems)
            {
                TemplatePicker.Remove(selectedItem);
            }

            // workaround for WPF quirk when checkbox is not visible, the Checked event does not fire
            SelectedItems = TemplatePicker.SelectedItems;
            Refresh();

            if (index >= SelectedTemplatesListBox.Items.Count)
            {
                index = SelectedTemplatesListBox.Items.Count - 1;
            }

            if (index >= 0)
            {
                SelectedTemplatesListBox.SelectedIndex = index;
            }
        }
        public void Initialize([NotNull] string title, [NotNull] DatabaseUri databaseUri, [NotNull] IEnumerable <ItemId> selectedTemplates, bool includeBranches = false)
        {
            Assert.ArgumentNotNull(title, nameof(title));
            Assert.ArgumentNotNull(databaseUri, nameof(databaseUri));
            Assert.ArgumentNotNull(selectedTemplates, nameof(selectedTemplates));

            Title         = title;
            SelectedItems = new List <ItemId>(selectedTemplates);

            TemplatePicker.Initialize(databaseUri, selectedTemplates, includeBranches);
        }
        private void SetSelection([NotNull] object sender, [NotNull] SelectionChangedEventArgs e)
        {
            Debug.ArgumentNotNull(sender, nameof(sender));
            Debug.ArgumentNotNull(e, nameof(e));

            var selectedItem = SelectedTemplatesListBox.SelectedItems.OfType <ListBoxItem>().Select(listBoxItem => listBoxItem.Tag).OfType <TemplateHeader>().Select(templateHeader => templateHeader.TemplateId).FirstOrDefault();

            if (selectedItem == null)
            {
                return;
            }

            TemplatePicker.FocusItem(selectedItem);
        }
        private void Refresh()
        {
            SelectedTemplatesListBox.Items.Clear();

            foreach (var selectedItem in SelectedItems)
            {
                var templateHeader = TemplatePicker.GetTemplateHeader(selectedItem);
                if (templateHeader == null)
                {
                    continue;
                }

                var listBoxItem = new ListBoxItem
                {
                    Tag     = templateHeader,
                    Content = templateHeader.Name
                };

                SelectedTemplatesListBox.Items.Add(listBoxItem);
            }
        }