title: Can Generics be used with Silverlight?
body: <p>I have a user control that displays objects of a client-declared type with some specialized behavior. I would like to use generics. However, I'm not sure how to declare this in the XAML:</p>
  
  <pre><code>&lt;local:EditableListBox x:Name="SectionList" Margin="56,8,15,0" FontSize="64" SelectionChanged="SectionList_SelectionChanged" /&gt;
  </code></pre>
  
  <p><code>ListBox</code> uses <code>object</code> members, which makes me think that perhaps there's no way to have type safety here. Or is there?</p>
  
  <p>(I'm building a Windows Phone 7 app, if it makes any difference.)</p>
  
  <p><strong>Update</strong>: I'm totally fine with not having generics in the XAML, but I'm still trying to figure out how to set it up in the code-behind. I parameterized everything, but it's still complaining.</p>
  
  <p>Code behind:</p>
  
  <pre><code>public partial class EditableListBox&lt;T&gt; : UserControl, INotifyPropertyChanged where T : IEditableListMember {
  
      public EditableListBox()
      {
          // Error: The name 'InitializeComponent' does not exist in the current context
          InitializeComponent();
          Loaded += new RoutedEventHandler(EditableListBox_Loaded);
      }
  
      // ...
  
      public int SelectedIndex
      {
          get
          {
              // Error: The name 'ContentListBox' does not exist in the current context   
              return ContentListBox.SelectedIndex;
          }
          set
          {
              // Error: The name 'ContentListBox' does not exist in the current context   
              ContentListBox.SelectedIndex = value;
          }
      }
  </code></pre>
  
  <p>The XAML:</p>
  
  <p></p>
  
  <pre><code>&lt;Grid x:Name="LayoutRoot"&gt;
      &lt;ListBox x:Name="ContentListBox"&gt;
          &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;Grid ManipulationCompleted="Grid_ManipulationCompleted"&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width="Auto"/&gt;
                          &lt;ColumnDefinition Width="*" /&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
  
                      &lt;Image Source="{Binding IconSource}"
                             Grid.Column="0"
                             Width="96"
                             Height="96"
                             VerticalAlignment="Center"
                             Visibility="{Binding Editing, Converter={StaticResource visibilityConverter}}"
                             /&gt;
  
                      &lt;TextBlock Text="{Binding Name}" 
                                 Grid.Column="1" 
                                 Foreground="{Binding Enabled, Converter={StaticResource enabledConverter}}" /&gt;
  
                  &lt;/Grid&gt;
              &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
      &lt;/ListBox&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>It gives two compiler errors: the <code>ListBox</code> <code>ContentListBox</code> and <code>InitializeComponent()</code> are not defined. I suspect that the problem has something to do with how the class is split into two <code>partial</code> definitions, and mine is parameterized whereas the generated code is not. How can I get around this?</p>