title: WPF filtered dynamic menu
body: <p>I'm working on getting a UserControl in WPF working that has a MenuItem populated with an ItemsSource, which creates a menu that goes n levels deep (although I'm just looking at TopMenuItem\Branches\Leaves right now).
  The wrinkle I'm having trouble with is that I want to filter the leaves through a textbox embedded into the menu. If a branch has no leaves, it also gets filtered out. It looks like this at the moment :</p>
  <p><img src="https://i.stack.imgur.com/q26S4.png" alt="It looks like this at the moment" /></p>
  <p>I'm working with an ObservableCollection of IMenuTreeItem, which can contain branches (which in turn also has an ObservableCollection of IMenuTreeItem) or leaves.</p>
  <pre><code>public interface IMenuTreeItem
  {
      string Name { get; set; }
  }
  
  public class MenuTreeLeaf : IMenuTreeItem
  {
      public string Name { get; set; }
      public Guid UID { get; set; }
      public ObjectType Type { get; set; }
      public Requirement Requirement { get; set; }
  
      public MenuTreeLeaf(string name, ObjectType type, Guid uID)
      {
          Type = type;
          Name = name;
          UID = uID;
      }
      public MenuTreeLeaf(string name)
      {
          Name = name;
      }
  }
  public class MenuTreeBranch : IMenuTreeItem, INotifyPropertyChanged
  {
      public string Name { get; set; }
      private ObservableCollection&lt;IMenuTreeItem&gt; _items;
      public ObservableCollection&lt;IMenuTreeItem&gt; Items
      {
          get
          {
              return _items;
          }
          set
          {
              _items = value; OnPropertyChanged();
          }
      }
  
      public MenuTreeBranch(string name)
      {
          Name = name;
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected void OnPropertyChanged(string name = null)
      {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
      }
  }
  </code></pre>
  <p>This is how I'm filtering. It very much feels like there's a better way.</p>
  <pre><code>            ObservableCollection&lt;IMenuTreeItem&gt; result = new ObservableCollection&lt;IMenuTreeItem&gt;(ItemsSource);
              for (int i = 0; i &lt; result.Count; i++)
              {
                  if (result[i] is MenuTreeBranch currentBranch)
                  {
                      if (currentBranch.Items != null)
                          currentBranch.Items = new ObservableCollection&lt;IMenuTreeItem&gt;(currentBranch.Items.Where(x =&gt; x.Name.ToLower().Contains(SearchField.ToLower())));
                  }
              }
              result = new ObservableCollection&lt;IMenuTreeItem&gt;(result.Where(x =&gt; (x as MenuTreeBranch).Items.Count &gt; 0));
              result.Insert(0, new MenuTreeLeaf(&quot;[Search]&quot;));
              return result;
  </code></pre>
  <p>So my main problems are:</p>
  <ul>
  <li>When I've filtered, I can no longer unfilter. ItemsSource gets changed too. Could it be because I'm filtering in the ItemsSourceFiltered getter? I tried to clone, but eh, didn't change anything</li>
  <li>When I call OnPropertyChanged on ItemsSourceFiltered any time text changes in the textbox, the menu closes. The menu definitely shouldn't close while you're inputting text.</li>
  </ul>
  <p>Any advice?</p>