title: Using MVVM instead of MultiValueConverter in WPF XAML
body: <p>I've been working with XAML for some time now, but when I started I didn't knew much about MVVM so I avoided it. So all the logic in my projects is written in .xaml.cs instead of ViewModel.cs. Now the project is big and I have to move some parts of it to MVVM per request and I have no clue how to do so or where to actually start.</p>
  <p>about the problem:</p>
  <p>I have two Entities. lets call them A and Secret.
  A can have Secret. If A does have it I need to display Secret.Name and Secret.Description beside the A data.
  They are binded together with: A.Secret == Secret.Id.</p>
  <p>A data and B data can change independently and is received/changed over API calls, but A.Secret and Secret.Id bind will never change.</p>
  <p>Secrets is just an array of SecretModel elements</p>
  <pre><code>&lt;ListBox ItemsSource=&quot;{Binding A, ElementName=uc}&quot; SelectedItem=&quot;{Binding SelectedA, ElementName=uc}&quot;&gt;
                          &lt;ListBox.ItemTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;Grid VerticalAlignment=&quot;Center&quot;&gt;
                                      &lt;Grid.RowDefinitions&gt;
                                          &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                                          &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                                          &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                                          &lt;RowDefinition Height=&quot;Auto&quot; MaxHeight=&quot;300&quot;/&gt;
                                      &lt;/Grid.RowDefinitions&gt;
                                      &lt;Grid.ColumnDefinitions&gt;
                                          &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                                          &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
                                      &lt;/Grid.ColumnDefinitions&gt;
  
                                      &lt;TextBlock Text=&quot;ID:&quot; TextAlignment=&quot;Right&quot; /&gt;
                                      &lt;TextBlock Grid.Column=&quot;1&quot; Text=&quot;{Binding Id}&quot;/&gt;
                                      &lt;TextBlock Grid.Row=&quot;1&quot; Text=&quot;Type:&quot; TextAlignment=&quot;Right&quot;/&gt;
                                      &lt;TextBlock Grid.Row=&quot;1&quot; Grid.Column=&quot;1&quot; Text=&quot;{Binding Type}&quot; /&gt;
                                      &lt;TextBlock Grid.Row=&quot;2&quot; Text=&quot;Secret:&quot; TextAlignment=&quot;Right&quot; Visibility=&quot;{Binding Secret, Converter={StaticResource StringToVisibilityConverter}}&quot;/&gt;
                                      &lt;TextBlock Grid.Row=&quot;2&quot; Grid.Column=&quot;1&quot; Visibility=&quot;{Binding Secret, Converter={StaticResource StringToVisibilityConverter}}&quot;&gt;
                                          &lt;TextBlock.Text&gt;
                                              &lt;MultiBinding Converter=&quot;{StaticResource SecretIdToNameConverter}&quot;&gt;
                                                  &lt;Binding Path=&quot;Secret&quot;/&gt;
                                                  &lt;Binding Path=&quot;Secrets&quot; ElementName=&quot;uc&quot;/&gt;
                                              &lt;/MultiBinding&gt;
                                          &lt;/TextBlock.Text&gt;
                                          &lt;TextBlock.ToolTip&gt;
                                              &lt;MultiBinding Converter=&quot;{StaticResource SecretIdToDescriptionConverter}&quot;&gt;
                                                  &lt;Binding Path=&quot;Secret&quot;/&gt;
                                                  &lt;Binding Path=&quot;Secrets&quot; ElementName=&quot;uc&quot; /&gt;
                                              &lt;/MultiBinding&gt;
                                          &lt;/TextBlock.ToolTip&gt;
                                      &lt;/TextBlock&gt;
                                      
                              &lt;/DataTemplate&gt;
                          &lt;/ListBox.ItemTemplate&gt;
                          
                      &lt;/ListBox&gt;
  </code></pre>
  <p>Converters looks like this.</p>
  <pre><code>public class SecretIdToDescriptionConverter : IMultiValueConverter
  {
      public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
      {
          if (values[0] != null &amp;&amp; values[0] != DependencyProperty.UnsetValue &amp;&amp; values[0] is string SecretId &amp;&amp; values[1] != null &amp;&amp; values[1] != DependencyProperty.UnsetValue &amp;&amp; values[1] is SecretModel[] Secrets)
          {
              var tmp = Secrets.FirstOrDefault(secret =&gt; secret?.Id == SecretId);
              if (tmp != null)
                  return tmp.Description;
              return null;
          }
          else
          {
              return null;
          }
      }
  
      public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  <p>If anyone could give me some help/pointers on how I should/could migrate from Converters of this type to MVVM or point me to some resource explaining it to me - that would be greatly appreciated.</p>
  <p>The things that mostly boggles me:
  How should I share/acces the data betwean .xaml.cs or .xaml and ViewModel.cs at this point?
  How are calls made to retrieve/give such data?
  Is it possible to create method that would return the corresponding SecretModel, so I could just use it after the call in .xaml?
  like</p>
  <pre><code>&lt;magic ItemsSource= getSecret(A.Secret) &gt;
         &lt;TextBlock Text = {Binding Name}/&gt;
  &lt;/magic&gt;
  </code></pre>