title: Problem databinding a custom control in silverlight
body: <p>I'm attempting to databind a custom control in Silverlight 3 and I'm getting strange problems with it.</p>
  
  <p>My xaml for the user control is this:</p>
  
  <pre><code>&lt;UserControl x:Class="StronicoMain.GenericSmallIcon"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Width="400" Height="300" &gt;
      &lt;Canvas x:Name="canGSI"&gt;
  
      &lt;/Canvas&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>The codebehind for the user control is this</p>
  
  <p>private string _EntityTypeID;</p>
  
  <pre><code>    public string EntityTypeID
      {
          get
          {
              return _EntityTypeID;
          }
  
          set
          {
              _EntityTypeID = value;
          }
      }
  
      public GenericSmallIcon()
      {
          InitializeComponent();
          Loaded += new RoutedEventHandler(Page_Loaded);
      }        
      public void Page_Loaded(object sender, RoutedEventArgs e)
      {
          icoMale icoMale = new icoMale();
          icoFemale icoFem = new icoFemale();
  
          if (EntityTypeID == null)
          {
              canGSI.Children.Add(icoMale);
          }
          else if (EntityTypeID == "1")
          {
              canGSI.Children.Add(icoMale);
          }
          else if (EntityTypeID == "2")
          {
              canGSI.Children.Add(icoFem);
          }
          else
          {
              canGSI.Children.Add(icoMale);
          }            
      }
  </code></pre>
  
  <p>I'm calling it from the DataGridSelection Adapter (taken from the Microsoft Toolkit example page for AutoCompleteBox-Datagrid version) - the relevant portion looks like this:</p>
  
  <pre><code>&lt;Stron:DataGridSelectionAdapter x:Name="SelectionAdapter" AutoGenerateColumns="False" IsReadOnly="False"&gt;
                                                      &lt;Stron:DataGridSelectionAdapter.Columns&gt;
                                                          &lt;data:DataGridTemplateColumn&gt;
                                                              &lt;data:DataGridTemplateColumn.CellTemplate&gt;
                                                                  &lt;DataTemplate&gt;&lt;Stron:GenericSmallIcon EntityTypeID="{Binding EntityTypeID}"&gt;&lt;/Stron:GenericSmallIcon&gt;&lt;/DataTemplate&gt;
                                                              &lt;/data:DataGridTemplateColumn.CellTemplate&gt;
                                                          &lt;/data:DataGridTemplateColumn&gt;
                                                              &lt;data:DataGridTextColumn Header="Contact Name" FontWeight="Bold" Foreground="#CC000000" Binding="{Binding EntityName}" /&gt;
                                                          &lt;data:DataGridTextColumn Header="Tags" Binding="{Binding EntityTags}" /&gt;                                                        
                                                      &lt;/Stron:DataGridSelectionAdapter.Columns&gt;
                                                  &lt;/Stron:DataGridSelectionAdapter&gt;
  </code></pre>
  
  <p>I run the code, and I get the error "ManagedRuntimeError #4004" - if I try to use the custom control while manually setting the databinding it works just fine, if I try to rely on the values that are being databound I get the error.  How can I create a custom databound event on a custom control?  I think that is the problem, that the page is loading before the values are passed to it.</p>
  
  <p>Thanks everyone.</p>
  
  <p>~Steve</p>
  
  <hr>
  
  <p>Update, here is the working, changed code as per the accepted answer</p>
  
  <pre><code>public static readonly DependencyProperty EntityTypeIDProperty = DependencyProperty.Register("EntityTypeID", typeof(string), typeof(GenericSmallIcon), new PropertyMetadata(new PropertyChangedCallback(GenericSmallIcon.OnEntityTypeIDPropertyChanged)));
  public string EntityTypeID
  {
      get { return (string)GetValue(EntityTypeIDProperty); }
      set { SetValue(EntityTypeIDProperty, value); }
  }
  private static void OnEntityTypeIDPropertyChanged(
      DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
      GenericSmallIcon control = d as GenericSmallIcon;
      string b = (string)e.NewValue;
  }
  </code></pre>