title: Check if specified element is already the logical child of another element
body: <p>I a beginner in C# and WPF. I'm programming plugin for a node based software called vvvv. I have implemented sliders, buttons and other simple ui elements. The following code shows how a sliders node look in c# :</p>
  
  <pre><code>using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Markup;
  using System.Xml;
  using VVVV.PluginInterfaces.V2;
  
  namespace VVVV.Packs.UI.Nodes.WPF
  {
      [PluginInfo(Author = "lecloneur", Category = "WPF", Help = "WPF Slider", Name = "Slider", AutoEvaluate = false)]
  
      public class WPFSlider : GenericNode, IPluginEvaluate
      {
          [Input("SizeX", DefaultValue = 120, Order = 9, MinValue = 0)]
          public IDiffSpread&lt;int&gt; SizeX;
  
          [Input("SizeY", DefaultValue = 120, Order = 9, MinValue = 0)]
          public IDiffSpread&lt;int&gt; SizeY;
  
          [Input("Orientation", Order = 1, DefaultEnumEntry = "Horizontal")]
          public IDiffSpread&lt;Orientation&gt; OrientationIn;
  
          [Output("Value", Order = 2)]
          public ISpread&lt;double&gt; ValueOut;
  
          int elements_count = 0;
  
          public void Evaluate(int SpreadMax)
          {
              UIElementOut.SliceCount = SpreadMax;
              ValueOut.SliceCount = SpreadMax;
              for (int i = 0; i &lt; SpreadMax; i++)
              {
                  if (UIElementOut == null || !(UIElementOut[0] is Slider) || elements_count &lt; SpreadMax || OrientationIn.IsChanged || SizeX.IsChanged || SizeY.IsChanged)
                  {
                      CreateElement(i);
                  }
                  OutputData(i);
                  Transformation(i, (Slider)UIElementOut[i]);
              }
              elements_count = SpreadMax;
          }
  
          private void CreateElement(int i)
          {
              UIElementOut[i] = new Slider();
              var uiElement = (Slider)UIElementOut[i];
              uiElement.Minimum = 0;
              uiElement.Maximum = 1;
              uiElement.Orientation = OrientationIn[i];
              uiElement.IsMoveToPointEnabled = true;
              uiElement.Width = SizeX[i]; ;
              uiElement.Height = SizeY[i];
              uiElement.VerticalAlignment = VerticalAlignment.Center;
              uiElement.HorizontalAlignment = HorizontalAlignment.Center;
  
              XmlReader XmlRead = XmlReader.Create("Styles/SliderStyle.xaml");
              ResourceDictionary myResourceDictionary = (ResourceDictionary)XamlReader.Load(XmlRead);
              XmlRead.Close();
              Style uiElementStyle = myResourceDictionary["SliderStyle"] as Style;
              uiElement.Style = uiElementStyle;
          }
  
          private void OutputData(int i)
          {
              var uiElement = (Slider)UIElementOut[i];
              ValueOut[i] = uiElement.Value;
          }
      } 
  }
  </code></pre>
  
  <p>Now I'm trying to implement a tabcontrol where I could dynamically create tabitem and input UIElement into it. As far as I understand, I can only add one things to a tabitem. So I was thinking about creating a grid everytime I need to and fill it with all the incoming UIElement.</p>
  
  <pre><code>    public void Evaluate(int SpreadMax)
      {
          SpreadMax = 1;
  
          UIElementOut.SliceCount = 1;
  
          for (var i = 0; i &lt; SpreadMax; i++)
          {
              if (UIElementOut == null || !(UIElementOut[i] is TabControl))
                  UIElementOut[i] = new TabControl();
                  var uiElement = (TabControl)UIElementOut[i];
                  uiElement.Height = 200;
                  uiElement.Width = 500;
          }
  
  
  
          Grid grid;
  
          int[] _elementCounts = new int[_elementInputs.SliceCount];
  
          for (var i = 0; i &lt; _elementInputs.SliceCount; i++)
          {
              if (_elementInputs[i] == null || !(_elementInputs[i] is UIElement))
              {
                  grid = new Grid();
  
                  for (var j = 0; j &lt; _elementInputs[i].IOObject.SliceCount; j++)
                  {
                      if (_elementInputs[i].IOObject[j] != null)
                      {
                          UIElement test = new UIElement();
                          test = _elementInputs[i].IOObject[j];
                          grid.Children.Add(test);
                      }
                  }
                  _elementCounts[i] = _elementInputs[i].IOObject.SliceCount;
                  ValueOut[i] = _elementCounts[i];
  
  
                  if (((TabControl)UIElementOut[0]).Items.Count &lt;= i)
                  {
                      ((TabControl)UIElementOut[0]).Items.Add(new TabItem { Header = _nameInputs[i].IOObject[0], Content = grid });
                  }
  
                  if (_nameInputs[i].IOObject.IsChanged)
                  {
                      ((TabItem)((TabControl)UIElementOut[0]).Items[i]).Header = _nameInputs[i].IOObject[0];
                  }
  
                  if (_elementInputs[i].IOObject.IsChanged)
                  {
                      ((TabItem)((TabControl)UIElementOut[0]).Items[i]).Content = grid;
                  }
              }
          }
  
          for (var i = ((TabControl)UIElementOut[0]).Items.Count - 1; ((TabControl)UIElementOut[0]).Items.Count &gt; _elementInputs.SliceCount; i--)
          {
              ((TabControl)UIElementOut[0]).Items.RemoveAt(i);
          }
      }
  </code></pre>
  
  <p>I searched a lot but can't find any idea how to solve the error. Apparently adding elements to a the grid throw "specified element is already the logical child of another element"; </p>