private void InvalidateTokensInternalList() { Tokens.Clear(); var id = 1; foreach (var item in ItemsSource) { var tokenInfo = new TokenInfo { Id = ++id, Content = item, Visibility = Visibility.Visible }; Tokens.Add(tokenInfo); } }
private void ToggleContainerVisibility(FrameworkElement container, TokenInfo tokenInfo) { if (container == null || tokenInfo == null) { return; } var searchFields = container.ChildrenOfType<TextBlock>(); if (searchFields == null || searchFields.Count <= 0) { tokenInfo.Visibility = Visibility.Collapsed; return; } var isFilterContained = false; var highlightingForeground = HighlightingForeground; var highlightingBackground = this.HighlightingBackground; foreach (var field in searchFields) { if (TokenEditExtensions.GetIsExcludedFromSearch(field)) { continue; } // clear any previously applied formatting var contentRange = new TextRange(field.ContentStart, field.ContentEnd); contentRange.ClearAllProperties(); var matches = Regex.Matches(field.Text, Filter, RegexOptions.IgnoreCase); if (matches.Count == 0) { continue; } isFilterContained = true; foreach (Match match in matches) { var textRange = field.GetTextRangeFromCharOffset(match.Index, match.Length); textRange.ApplyPropertyValue(TextElement.BackgroundProperty, highlightingBackground); if (highlightingForeground != null) { textRange.ApplyPropertyValue(TextElement.ForegroundProperty, highlightingForeground); } } } tokenInfo.Visibility = isFilterContained ? Visibility.Visible : Visibility.Collapsed; }
/// <summary> /// Handle changes in the source collection /// </summary> private void OnSourceSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: { var id = Tokens.Max(ti => ti.Id); foreach (var newToken in e.NewItems) { var tokenInfo = new TokenInfo { Id = ++id, Content = newToken, Visibility = Visibility.Visible }; Tokens.Add(tokenInfo); } break; } case NotifyCollectionChangedAction.Remove: { foreach (var oldToken in e.OldItems) { var tokenInfo = Tokens.FirstOrDefault(ti => ti.Content == oldToken); if (tokenInfo == null) { continue; } Tokens.Remove(tokenInfo); } break; } case NotifyCollectionChangedAction.Reset: InvalidateTokensInternalList(); break; default: throw new InvalidOperationException(); } }