title: WPF Custom Control - How do you bind a listbox from a static ObservableCollection?
body: <p>I'm not sure how to go about this exactly. I have a static ObservableCollection in the NameField.cs class. I just have no idea how to bind it to the listbox.</p>
  
  <ul>
  <li>Should I not be using a ListBox?</li>
  <li>Should I be using a DependencyProperty?</li>
  <li>Should I be exposing the ObservableCollection through a property or by public?</li>
  </ul>
  
  <p>I'm not sure what to do here...</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.ComponentModel;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Media;
  
  namespace MyWPF
  {
  
      [TemplatePart(Name = NameField.ElementPrefixBox, Type = typeof(ListBox))]
      public class NameField : Control
      {
          private const String ElementPrefixBox        = "PART_PrefixBox";
  
          private static ObservableCollection&lt;NamePrefix&gt; _namePrefixes;
  
          static NameField()
          {
              DefaultStyleKeyProperty.OverrideMetadata(typeof(NameField), new FrameworkPropertyMetadata(typeof(NameField)));
  
              _namePrefixes = new ObservableCollection&lt;NamePrefix&gt;();
          }
  
          public static void AddNamePrefix(NamePrefix namePrefix)
          {
              lock (_namePrefixes)
              {
                  _namePrefixes.Add(namePrefix);
              }
          }
  
          public static IEnumerator&lt;NamePrefix&gt; GetNamePrefixes()
          {
              return _namePrefixes.GetEnumerator();
          }
  
      }
  
      /// &lt;summary&gt;
      /// A Key/Value structure containing a Name Prefix ID and String value.
      /// &lt;/summary&gt;
      public struct NamePrefix
      {
          public NamePrefix(Int32 id, String prefix)
              : this()
          {
              ID = id;
              Prefix = prefix;
          }
  
          public Int32 ID { get; set; }
          public String Prefix { get; set; }
      }
  
  }
  
  &lt;ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:MyWPF"
      xmlns:con="clr-namespace:MyWPF.Converters"
      &gt;
  
      &lt;Style TargetType="{x:Type local:NameField}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type local:NameField}"&gt;
                      &lt;StackPanel Orientation="Horizontal"&gt;
                          &lt;TextBlock TextWrapping="NoWrap" Text="Name:" VerticalAlignment="Center" Margin="3" /&gt;
                          &lt;ListBox x:Name="PART_PrefixBox" VerticalAlignment="Center" Margin="3" &gt;
                              &lt;ListBox.ItemBindingGroup&gt;
                                  &lt;BindingGroup Name="NamePrefixes"/&gt;
                              &lt;/ListBox.ItemBindingGroup&gt;
                          &lt;/ListBox&gt;
                      &lt;/StackPanel&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>