/// <summary> /// Вспомогательная функция для режима выбора одного элемента, задает состояние IsSelected если оно указано и заполняет список коллекции UnselectedItems /// </summary> /// <remarks> /// Helper function that sets items state if state value is specified, and filled UnselectedItems collection /// This function is used in Single item Selection Mode /// </remarks> private void FillUnselected(object excludedValue, bool setItemsToState = false) { foreach (var item in Items) { if (!Equals(item, excludedValue)) { var itemContainer = GetItemContainer(item); if (itemContainer != null) { if (!itemContainer.IsSelected && !UnselectedItems.Contains(item)) { UnselectedItems.Add(item); } else if (itemContainer.IsSelected && !UnselectedItems.Contains(item)) { if (setItemsToState) { itemContainer.IsSelected = false; if (!itemContainer.IsSelected) { UnselectedItems.Add(item); } } } else if (itemContainer.IsSelected && UnselectedItems.Contains(item)) { if (setItemsToState) { itemContainer.IsSelected = false; } } } } } }
/// <summary> /// Задает свойства контейнера элемента для отображения, /// а так же устанавливает значения IsSelected если задано свойство SelectedIndex или SelectedItem /// </summary> /// <remarks> /// Prepare the element to display the item, /// depending on SelectionMode of elements and specified SelectedIndex or SelectedItem /// </remarks> protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { base.PrepareContainerForItemOverride(element, item); if (SelectedIndex == -1 && SelectedItem == null) { if (SelectionMode == SelectionModeEnum.MultipleWithDefault) { if (Items.IndexOf(item) == 0) { (element as UIEnumSwitchItem).MultipleSelectionWithDefault(true, SelectionAction.FirstSelection); } else { if (!UnselectedItems.Contains(item)) { UnselectedItems.Add(item); } } } } else if (SelectedIndex != -1) { if (SelectedIndex == Items.IndexOf(item)) { if (SelectionMode == SelectionModeEnum.Single) { (element as UIEnumSwitchItem).SingleSelection(true, SelectionAction.FirstSelection); } else if (SelectionMode == SelectionModeEnum.MultipleWithDefault) { (element as UIEnumSwitchItem).MultipleSelectionWithDefault(true, SelectionAction.FirstSelection); } else if (SelectionMode == SelectionModeEnum.Multiple) { (element as UIEnumSwitchItem).MultipleSelection(true, SelectionAction.FirstSelection); } } else { if (!UnselectedItems.Contains(item)) { UnselectedItems.Add(item); } } } else if (SelectedItem != null) { if (Equals(item, SelectedItem)) { if (SelectionMode == SelectionModeEnum.Single) { (element as UIEnumSwitchItem).SingleSelection(true, SelectionAction.FirstSelection); } else if (SelectionMode == SelectionModeEnum.MultipleWithDefault) { (element as UIEnumSwitchItem).MultipleSelectionWithDefault(true, SelectionAction.FirstSelection); } else if (SelectionMode == SelectionModeEnum.Multiple) { (element as UIEnumSwitchItem).MultipleSelection(true, SelectionAction.FirstSelection); } } else { if (!UnselectedItems.Contains(item)) { UnselectedItems.Add(item); } } } if (ShowDescriptionInTooltip) { if ((item as Enum).GetAttributeOfType <DescriptionAttribute>() != null) { var description = (item as Enum).GetAttributeOfType <DescriptionAttribute>().Description; if (description != string.Empty) { (element as UIEnumSwitchItem).ToolTip = description; } } } if (SeparateItems) { BorderThickness = new Thickness(0); (element as UIEnumSwitchItem).CornerRadius = CornerRadius; (element as UIEnumSwitchItem).BorderThickness = new Thickness(1); if (Items.Count == 1) { (element as UIEnumSwitchItem).Margin = new Thickness(0); } else if (Items.IndexOf(item) == 0) { (element as UIEnumSwitchItem).Margin = new Thickness(0, 0, 2, 0); } else if (Items.IndexOf(item) == Items.Count - 1) { (element as UIEnumSwitchItem).Margin = new Thickness(2, 0, 0, 0); } else { (element as UIEnumSwitchItem).Margin = new Thickness(2, 0, 2, 0); } } else { (element as UIEnumSwitchItem).Margin = new Thickness(0); if (Items.Count == 1) { (element as UIEnumSwitchItem).CornerRadius = CornerRadius; (element as UIEnumSwitchItem).BorderThickness = new Thickness(1); } else if (Items.IndexOf(item) == 0) { (element as UIEnumSwitchItem).CornerRadius = new CornerRadius(CornerRadius.TopLeft, 0d, 0d, CornerRadius.BottomLeft); (element as UIEnumSwitchItem).BorderThickness = new Thickness(1, 1, 0, 1); } else if (Items.IndexOf(item) == Items.Count - 1) { (element as UIEnumSwitchItem).CornerRadius = new CornerRadius(0d, CornerRadius.TopRight, CornerRadius.BottomRight, 0d); (element as UIEnumSwitchItem).BorderThickness = new Thickness(0, 1, 1, 1); } else { (element as UIEnumSwitchItem).CornerRadius = new CornerRadius(0); (element as UIEnumSwitchItem).BorderThickness = new Thickness(0, 1, 0, 1); } } }