title: Silverlight Databinding - Binding a ValueConverter to a property on a view-model
body: <p>Let's pretend I have the following xaml...</p>
  
  <p></p>
  
  <pre><code>&lt;UserControl.Resources&gt;
      &lt;local:ViewModel x:Name="viewModel" /&gt;
      &lt;local:LoadChildrenValueConverter x:Name="valueConverter" /&gt;
  &lt;/UserControl.Resources&gt;
  
  &lt;UserControl.DataContext&gt;
      &lt;Binding Source="{StaticResource viewModel}" /&gt;
  &lt;/UserControl.DataContext&gt;
  
  &lt;Grid x:Name="LayoutRoot" Background="White"&gt;
      &lt;control:TreeView ItemsSource="{Binding Root}"&gt;
          &lt;control:TreeView.ItemTemplate&gt;
              &lt;control:HierarchicalDataTemplate ItemsSource="{Binding Converter={StaticResource valueConverter}}"&gt;
                  &lt;TextBlock Text="{Binding}" /&gt;
              &lt;/control:HierarchicalDataTemplate&gt;
          &lt;/control:TreeView.ItemTemplate&gt;
      &lt;/control:TreeView&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>...and the following code to go with it...</p>
  
  <pre>
  using System;
  using System.Collections.ObjectModel;
  using System.Windows.Data;
  
  namespace SilverlightViewModelSpike
  {
      public class ViewModel
      {
          public ViewModel()
          {
              Root = new ObservableCollection() { "Item 1", "Item 2", "Item 3", };
          }
  
          public ObservableCollection Root { get; private set; }        
      }
  
      public class LoadChildrenValueConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              return new ObservableCollection() { "Item 1", "Item 2", "Item 3", };
          }
  
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
  }
  </pre>
  
  <p>This works as expected, but it feels wrong that I have two separate classes that are required in order to grab the needed data for my view (imagine that ViewModel and LoadChildrenValueConverter pulled data from a web service instead of returning hard coded data). Is there a better solution here? I was thinking maybe something like this...</p>
  
  <pre>
  using System;
  using System.Collections.ObjectModel;
  using System.Windows.Data;
  
  namespace SilverlightViewModelSpike
  {
      public class ViewModel
      {
          public ViewModel()
          {
              Root = new ObservableCollection() { "Item 1", "Item 2", "Item 3", };
              ValueConverter = new LoadChildrenValueConverter();
          }
  
          public ObservableCollection Root { get; private set; }
          public LoadChildrenValueConverter ValueConverter { get; private set; }
      }
  
      public class LoadChildrenValueConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              return new ObservableCollection() { "Item 1", "Item 2", "Item 3", };
          }
  
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
  }
  </pre>
  
  <p>... but then i can't get this line to work...</p>
  
  <p><code>&lt;control:HierarchicalDataTemplate ItemsSource="{???}"</code>></p>
  
  <p>...and even that doesn't seem like a great solution. Does anyone have a nice clean solution for this?</p>