title: How to bind data from Dictionary to xaml and use selected object
body: <h2>CategoriesView.xaml</h2>
  
  <pre><code>&lt;local:AllViewBase x:Class="Test.View.Tab.CategoriesView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:local="clr-namespace:Test.View.Abstract"
               mc:Ignorable="d"&gt;
  
      &lt;UserControl.Resources&gt;
          &lt;ResourceDictionary Source="pack://application:,,,/Content/Style/MainWindowDictionary.xaml"/&gt;
      &lt;/UserControl.Resources&gt;
  
      &lt;Grid&gt;
          &lt;ListBox ItemsSource="{Binding Path=dcCategory}" SelectedItem="{Binding Path=Category,Mode=TwoWay}"&gt;
              &lt;ListBox.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;StackPanel&gt;
                          &lt;StackPanel Orientation="Horizontal" &gt;
                              &lt;Button Content="Add Value" Command="{Binding Path=DataContext.AddValue, RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"/&gt;
                              &lt;TextBlock Text="{Binding Path=Key}"/&gt;
                          &lt;/StackPanel&gt;
                          &lt;ListBox ItemsSource="{Binding Path=Value}" /&gt;
                      &lt;/StackPanel&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
              &lt;ListBox.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;StackPanel Orientation="Horizontal" /&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ListBox.ItemsPanel&gt;
          &lt;/ListBox&gt;
      &lt;/Grid&gt; 
  &lt;/local:AllViewBase&gt;
  </code></pre>
  
  <h2>CategoriesViewModel.cs</h2>
  
  <pre><code>namespace Test.ViewModel.Tab
  {
      public class CategoriesViewModel : AllViewModel&lt;CategoryValueAllView&gt;
      {
          private BaseCommand _AddValue;
  
          private Category _Category;
          public Category Category
          {
              get
              {
                  return _Category;
              }
              set
              {
                  if (_Category != value)
                  {
                      _Category = value;
                      OnPropertyChanged(() =&gt; Category);
                  }
              }
          }
  
          private Dictionary&lt;Category, List&lt;string&gt;&gt; _dcCategory;
          public Dictionary&lt;Category, List&lt;string&gt;&gt; dcCategory
          {
              get
              {
                  if (_dcCategory == null) LoadDictionary();
                  return _dcCategory;
              }
              set
              {
                  if (_dcCategory != value)
                  {
                      _dcCategory = value;
                      OnPropertyChanged(() =&gt; dcCategory);
                  }
              }
          }
  
          public ICommand AddValue
          {
              get
              {
                  if (_AddValue == null)
                  {
                      _AddValue = new BaseCommand(() =&gt; Messenger.Default.Send(CategoryValueCode.AddValue + "," + Category.CategoryId));
                  }
                  return _AddValue;
              }
          }
  
          public void LoadDictionary()
          {
              dcCategory = (from k in db.Category join w in db.CategoryVal on k.CategoryId equals w.CategoryId select new CategoryValueAllView { Category = w.Category, Val = w.Val }).GroupBy(x =&gt; x.Category).ToDictionary(grp =&gt; grp.Key, grp =&gt; grp.Select(obj =&gt; obj.Val).ToList());
          }
      }
  }
  </code></pre>
  
  <p>My goals are:</p>
  
  <ul>
  <li><code>&lt;TextBlock Text="{Binding Path=Key}"/&gt;</code> show Category.Name. I don't
  know how to use Key like an object and use property Name</li>
  <li>when I push the button 'Add Value' I want to set 'CategoryId' in 'Category' object and sent it throught 'AddValue' Command by
  Messenger</li>
  </ul>
  
  <p>Earlier I used the string category.name instead of the category object for dictionary key. Then <code>&lt;TextBlock Text="{Binding Path=Key}"/&gt;</code> showing correct name, but after all I was unable to pass the 'category.name' to the 'AddValue' command after click button to get id by name and set data by Messenger.</p>
  
  <p>Getting id by the name is acceptable too, the Category object as a key is not required</p>
  
  <p>in spite of everything I would like to learn it, but I can not stand an example which has show such a problem</p>
  
  <p>Thanks!</p>