title: WPF ListBox Binding ItemsSource
body: <p>In a <a href="https://stackoverflow.com/questions/4100690/binding-in-wpf-to-element-of-array-specified-by-property">related question</a> I asked about binding to a particular element of an array indexed by another property.  The answer provided worked really well for the sample code example provided.</p>
  
  <p>Where I'm running into trouble is that I specify an ItemSource for a ListBox and I get a DependencyProperty.UnsetValue in my converter when I step through it.  No doubt this is a problem with my understanding of Binding.  </p>
  
  <p>My ListBox looks like so:</p>
  
  <pre><code>&lt;ListBox ItemsSource="{Binding Path=MyList}"&gt;
      &lt;ListBox.Resources&gt;
          &lt;local:FoodIndexConverter x:Key="indexConverter" /&gt;
      &lt;/ListBox.Resources&gt;
      &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;TextBlock&gt;
                  &lt;TextBlock.Text&gt;
                      &lt;MultiBinding Converter="{StaticResource indexConverter}"&gt;
                          &lt;Binding Path="MyIndex" /&gt;
                          &lt;Binding Path="Fields" /&gt;
                      &lt;/MultiBinding&gt;
                  &lt;/TextBlock.Text&gt;
              &lt;/TextBlock&gt;
          &lt;/DataTemplate&gt;
      &lt;/ListBox.ItemTemplate&gt;
  &lt;/ListBox&gt;
  </code></pre>
  
  <p>and the code behind looks as so:</p>
  
  <pre><code>public MainWindow()
  {
      InitializeComponent();
      MyList.Add(new SomeData() { Fields = new object[] {"liver", "onions", "cake" } } );
      MyList.Add(new SomeData() { Fields = new object[] {"liver", "onions", "candy" } } );
      MyList.Add(new SomeData() { Fields = new object[] {"liver", "onions", "pie" } } );
  
      DataContext = this;
  }
  </code></pre>
  
  <p>MyList is a List.</p>
  
  <p>MyIndex is an int.</p>
  
  <p>The converter code is </p>
  
  <pre><code>    public class FoodIndexConverter : IMultiValueConverter
      {
          public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              if (values == null || values.Length != 2)
                  return null;
  
              int? idx = values[0] as int?;
              object[] food = values[1] as object[];
  
              if (!idx.HasValue || food == null)
                  return null;
  
              return food[idx.Value];
          }
  
          public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
  </code></pre>
  
  <p>When I step through the debugger in the converter code the MyIndex (value[0]) is <strong>DependencyProperty.UnsetValue</strong> - the object array is as I'd expect.  </p>
  
  <p>I'm assuming it is a Binding issue with:
      
  in that it doesn't know what MyIndex is.  </p>
  
  <p>If MyIndex were a property of the SomeData class, it works as I would expect but it's not, it's a property of the class MainWindow, just like MyList.</p>
  
  <p>How do I specify that I'm interested in the MyIndex property that is part of my DataContext and not the MyData List?  </p>