如何创建 TabControl Silverlight
看:
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>
代码:
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;
它抛出异常!!!
无法转换类型的对象 'Paradigma.Silverlight.DiccionarioDatos.Termino' 键入 System.Windows.Controls.TabItem'。
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'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题有点不清楚,但您无法将您的类
Termino
直接绑定到TabControl
的ItemsSource
,因为它不是从选项卡项
。您可以尝试将您的声明更改为:
我认为这应该有效。
文档
TabControl.ItemsSource
并没有真正帮助它指向ItemsControl
页面(TabControl
继承自),因此示例是为此,而不是TabControl
。实际上,考虑一下,您应该创建一个
TabItems
列表以设置为TabControl
的ItemsSource
并将您的类绑定到 <代码>选项卡项目。Your question is a bit unclear but you can't bind your class
Termino
directly to theItemsSource
of theTabControl
as it's not derived fromTabItem
.You can try changing your declaration to:
I think this should work.
The documentation for
TabControl.ItemsSource
doesn't really help has it points to theItemsControl
page (whichTabControl
inherits from) so the examples are for that rather thanTabControl
.Actually, thinking about it, you should be creating a list of
TabItems
to set to theItemsSource
of yourTabControl
and binding your class to theTabItem
.