如何创建 TabControl Silverlight

发布于 2024-10-12 18:34:45 字数 1395 浏览 3 评论 0原文

看:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2024-10-19 18:34:45

您的问题有点不清楚,但您无法将您的类 Termino 直接绑定到 TabControlItemsSource,因为它不是从 选项卡项

您可以尝试将您的声明更改为:

public class Termino : TabItem
{
    ....
}

认为这应该有效。

文档TabControl.ItemsSource 并没有真正帮助它指向 ItemsControl 页面(TabControl 继承自),因此示例是为此,而不是 TabControl

实际上,考虑一下,您应该创建一个 TabItems 列表以设置为 TabControlItemsSource 并将您的类绑定到 <代码>选项卡项目。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文