Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF TabControl:选项卡标题的字典_C#_Wpf_Data Binding - Fatal编程技术网

C# WPF TabControl:选项卡标题的字典

C# WPF TabControl:选项卡标题的字典,c#,wpf,data-binding,C#,Wpf,Data Binding,我有一个绑定到其viewmodel Items属性的TabControl: <TabControl ItemsSource="{Binding Items}"/> 那么如何编写绑定表达式呢 <TabControl ItemsSource="{Binding Items}"> <TabControl.ItemTemplate> <DataTemplate> <Label Content="{Bi

我有一个绑定到其viewmodel Items属性的TabControl:

<TabControl ItemsSource="{Binding Items}"/>
那么如何编写绑定表达式呢

<TabControl ItemsSource="{Binding Items}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding ????????}"/>
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>
我的DataTemplate,在标记中定义

您还可以覆盖已分类模型的ToString方法,并从中返回友好名称

公共阶层人士 { 公共字符串名{get;set;} 公共字符串LastName{get;set;} 公共重写字符串ToString { 返回人; } } 模板

我可以肯定的是,只需使用a将对象转换为适当的标题即可。然后你可以根据类型来决定。一个叫做Items的字典怎么样?然后在绑定到键的标题中,在选项卡中,可以使用绑定到值的content presenter。
Person p = new Person() { FirstName = "Alan", LastName = "Turing" };
Headers.Add(p, "Person");
Items.Add(p);
<TabControl ItemsSource="{Binding Items}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding ????????}"/>
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Car
{
    public string ModelName { get; set; }
    public string Manifacturer { get; set; }
}
    <DataTemplate DataType="{x:Type local:Person}">
        <StackPanel>
            <Label Content="{Binding FirstName}"/>
            <Label Content="{Binding LastName}"/>
        </StackPanel>
    </DataTemplate>

    <DataTemplate DataType="{x:Type local:Car}">
        <StackPanel>
            <Label Content="{Binding Manifacturer}"/>
            <Label Content="{Binding ModelName}"/>
        </StackPanel>
    </DataTemplate>