title: pass different data to each instance of usercontrol via xaml
body: <p>I need a TabControl whose TabItems are again TabControls. Tabitems of the sub-TabControls always contain a UserControl: CtrlRequest. I build this structure dynamically (based on some enums). The whole TabControl with its children is built and displayed correctly (see CtrlRestExt).</p>
  <p>The actual problem:</p>
  <p>Each ViewModel RequestVM instance (is the Datacontext of CtrlRequest) needs data of type RequestData to be supplied by the parent view (CtrlRestExt).</p>
  <p>I have defined a dependency property in RequestVM for this purpose.</p>
  <p>In CtrlRestExt I am trying to set this DP:
  <strong>&lt;local:CtrlRequest RequestData=&quot;{Binding}&quot; /&gt;</strong></p>
  <p>Unfortunately, the compiler does not recognize this property.</p>
  <p>Is my concept ok?
  How do I supply the individual instances of CtrlRequest with data?
  Or is it better to create everything by code (Tab-Tree, TabItem-Contents (Usercontrol with ViewModel)).</p>
  <pre><code>&lt;UserControl x:Class=&quot;CtrlRestExt&quot;
               xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
               xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
               xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
               xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
               xmlns:local=&quot;clr-namespace:PkaRestExtClient&quot;&gt;
  
      &lt;UserControl.DataContext&gt;
          &lt;local:RestExtVM /&gt;
      &lt;/UserControl.DataContext&gt;
  
      &lt;Grid&gt;
          &lt;TabControl Margin=&quot;8&quot;
                      Background=&quot;Black&quot;
                      BorderBrush=&quot;Black&quot;
                      BorderThickness=&quot;1&quot;
                      ItemsSource=&quot;{Binding ApiCategories.Values}&quot;&gt;
              &lt;TabControl.ItemTemplate&gt;
                  &lt;HierarchicalDataTemplate DataType=&quot;{x:Type local:ApiCategory}&quot;
                                            ItemsSource=&quot;{Binding TabItems.Values}&quot;&gt;
                      &lt;TextBlock Text=&quot;{Binding Title}&quot; /&gt;
                  &lt;/HierarchicalDataTemplate&gt;
              &lt;/TabControl.ItemTemplate&gt;
              &lt;TabControl.ContentTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;TabControl Margin=&quot;8&quot;
                                  Background=&quot;Black&quot;
                                  BorderBrush=&quot;Black&quot;
                                  BorderThickness=&quot;1&quot;
                                  ItemsSource=&quot;{Binding TabItems.Values}&quot;&gt;
                          &lt;TabControl.ItemTemplate&gt;
                              &lt;DataTemplate DataType=&quot;{x:Type local:RequestData}&quot;&gt;
                                  &lt;TextBlock Text=&quot;{Binding Title}&quot; /&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/TabControl.ItemTemplate&gt;
                          &lt;TabControl.ContentTemplate&gt;
                              &lt;DataTemplate DataType=&quot;{x:Type local:RequestData}&quot;&gt;
                                  &lt;local:CtrlRequest RequestData=&quot;{Binding}&quot; /&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/TabControl.ContentTemplate&gt;
                      &lt;/TabControl&gt;
                  &lt;/DataTemplate&gt;
              &lt;/TabControl.ContentTemplate&gt;
          &lt;/TabControl&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  
  &lt;UserControl x:Class=&quot;CtrlRequest&quot;
               xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
               xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
               xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
               xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
               xmlns:jlinq=&quot;clr-namespace:Newtonsoft.Json.Linq;assembly=Newtonsoft.Json&quot;
               xmlns:local=&quot;clr-namespace:PkaRestExtClient&quot;
               xmlns:i=&quot;http://schemas.microsoft.com/xaml/behaviors&quot;&gt;
  
      &lt;UserControl.DataContext&gt;
          &lt;local:RequestVM /&gt;
      &lt;/UserControl.DataContext&gt;
      ...
  &lt;/UserControl&gt;
  
  public class ApiCategory
  {
      public string Title { get; set; }
      public Dictionary&lt;Enum, RequestData&gt; TabItems { get; set; } = new Dictionary&lt;Enum, RequestData&gt;();
  }
  
  public class RequestData
  {
      public struct PkaGUID
      {
          public string User;
          public string LogisticProcess;
          public string Reservation;
      }
  
      public string Title { get; set; }
      public string SubURI { get; set; }
      public string PkaId;
      public string SinceTimestamp;
      public PkaGUID GUID;
  
      public ObservableCollection&lt;InputRow&gt; DataRows = new ObservableCollection&lt;InputRow&gt;();
  
      public RequestData(Enum SubCat)
      {
          Title = SubCat.ToString();
          SubURI = SubCat.Description;
      }
  }
  
  using Microsoft.VisualBasic;
  using System.ComponentModel;
  using System.Text.RegularExpressions;
  
  public class RestExtVM : ViewModelBase
  {
      private readonly Dictionary&lt;EnmRestExtCategories, Enum&gt; SelectedTab = new Dictionary&lt;EnmRestExtCategories, Enum&gt;();
      private readonly Dictionary&lt;string, string&gt; ParamSetting = new Dictionary&lt;string, string&gt;();
  
  
      public RestExtVM()
      {
          InitRequestData();
          InitSelectedTabs();
          InitParamSettings();
      }
  
  
      private Dictionary&lt;EnmRestExtCategories, ApiCategory&gt; _ApiCategories = new Dictionary&lt;EnmRestExtCategories, ApiCategory&gt;();
  
      public Dictionary&lt;EnmRestExtCategories, ApiCategory&gt; ApiCategories
      {
          get
          {
              return _ApiCategories;
          }
          set
          {
              _ApiCategories = Value;
              PropertyChanged(&quot;ApiCategories&quot;);
          }
      }
  
      private void InitRequestData()
      {
          _ApiCategories.Clear();
  
          foreach (var Cat in GetEnumValues&lt;EnmRestExtCategories&gt;())
              _ApiCategories.Add(Cat, new ApiCategory() { Title = Cat.Description });
  
          foreach (var SubCat in GetEnumValues&lt;EnmRestExtApiCategory&gt;())
              _ApiCategories[EnmRestExtCategories.API].TabItems.Add(SubCat, new RequestData(SubCat));
  
          foreach (var SubCat in GetEnumValues&lt;EnmRestExtAnnouncementCategory&gt;())
              _ApiCategories[EnmRestExtCategories.Announcements].TabItems.Add(SubCat, new RequestData(SubCat));
  
          foreach (var SubCat in GetEnumValues&lt;EnmRestExtBellCategory&gt;())
              _ApiCategories[EnmRestExtCategories.Bells].TabItems.Add(SubCat, new RequestData(SubCat));
  
          foreach (var SubCat in GetEnumValues&lt;EnmRestExtBoxStateCategory&gt;())
              _ApiCategories[EnmRestExtCategories.Boxstate].TabItems.Add(SubCat, new RequestData(SubCat));
  
          foreach (var SubCat in GetEnumValues&lt;EnmRestExtLockCategory&gt;())
              _ApiCategories[EnmRestExtCategories.Locks].TabItems.Add(SubCat, new RequestData(SubCat));
  
          foreach (var SubCat in GetEnumValues&lt;EnmRestExtLogisticCategory&gt;())
              _ApiCategories[EnmRestExtCategories.Logistic].TabItems.Add(SubCat, new RequestData(SubCat));
  
          foreach (var SubCat in GetEnumValues&lt;EnmRestExtNameplateCategory&gt;())
              _ApiCategories[EnmRestExtCategories.Nameplates].TabItems.Add(SubCat, new RequestData(SubCat));
  
          foreach (var SubCat in GetEnumValues&lt;EnmRestExtReservationCategory&gt;())
              _ApiCategories[EnmRestExtCategories.Reservations].TabItems.Add(SubCat, new RequestData(SubCat));
  
          foreach (var SubCat in GetEnumValues&lt;EnmRestExtSiteCategory&gt;())
              _ApiCategories[EnmRestExtCategories.Sites].TabItems.Add(SubCat, new RequestData(SubCat));
  
          foreach (var SubCat in GetEnumValues&lt;EnmRestExtUserCategory&gt;())
              _ApiCategories[EnmRestExtCategories.Users].TabItems.Add(SubCat, new RequestData(SubCat));
  
          var XXX = _ApiCategories[EnmRestExtCategories.Users].TabItems(EnmRestExtUserCategory.Add).DataRows;
  
          XXX.Add(new InputRow(&quot;pkaId&quot;, string.Empty));
          XXX.Add(new InputRow(&quot;gender&quot;, string.Empty));
          XXX.Add(new InputRow(&quot;name&quot;, string.Empty));
          XXX.Add(new InputRow(&quot;preName&quot;, string.Empty));
          XXX.Add(new InputRow(&quot;preName&quot;, string.Empty));
      }
  
  ...
  </code></pre>
  <p><a href="https://i.stack.imgur.com/cPVZM.png" rel="nofollow noreferrer">enter image description here</a></p>