0

Look:

TabControl def:

<sdk:TabControl x:Name="tcWords">
                <sdk:TabControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="{Binding Letra}" />
                            <TextBlock Text="{Binding Palabra}" />
                            <TextBlock Text="{Binding Palabra}" />
                        </StackPanel>
                    </DataTemplate>
                </sdk:TabControl.ItemTemplate>
            </sdk:TabControl>

Code:

public class Termino
    {
        public string Letra { get; set; }
        public string Palabra { get; set; }
        public string Significado { get; set; }
    }

  List<Termino> arrPalabras = new List<Termino>();
            arrPalabras.Add(new Termino { Letra = "A", Palabra = "Ave", Significado = "Cualquier cosa" });
            arrPalabras.Add(new Termino { Letra = "A", Palabra = "Avion", Significado = "Cualquier cosa avion" });
            //lstItems.ItemsSource = arrPalabras;
            tcWords.ItemsSource = arrPalabras;

It throws an exception!!!

Unable to cast object of type 'Paradigma.Silverlight.DiccionarioDatos.Termino' to type System.Windows.Controls.TabItem'.

Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
  • when are you getting this exception? Is this a `Binding` exception in the output window or does the debugger pukes? – VoodooChild Jan 20 '11 at 22:09

1 Answers1

1

Your question is a bit unclear but you can't bind your class Termino directly to the ItemsSource of the TabControl as it's not derived from TabItem.

You can try changing your declaration to:

public class Termino : TabItem
{
    ....
}

I think this should work.

The documentation for TabControl.ItemsSource doesn't really help has it points to the ItemsControl page (which TabControl inherits from) so the examples are for that rather than TabControl.

Actually, thinking about it, you should be creating a list of TabItems to set to the ItemsSource of your TabControl and binding your class to the TabItem.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • He is not binding his class but rather a list object `tcWords.ItemsSource = arrPalabras` – VoodooChild Jan 20 '11 at 21:53
  • @Voodoo - but `TabControl` wants a list of `TabItems` not a list of `RandomClass`. – ChrisF Jan 20 '11 at 21:54
  • ok, but setting the `ItemsSource` on the TabControl will create those `TabItems`. (but maybe not??) - I have to say I confused myself on this one :) – VoodooChild Jan 20 '11 at 22:04
  • @Voodoo - the error message states that it's trying to cast `Termino` to `TabItem` hence my answer, but your comment made me think some more. – ChrisF Jan 20 '11 at 22:06