我有以下XAML代码:
<TabControl x:Name="MainTabControl" ItemsSource="{Binding Projects}" Grid.Row="1">
<TabControl.ItemTemplate>
<DataTemplate>
<TabItem x:Name="ProjectTabItem" Header="{Binding ProjectName}">
<TextBox>This text doesn't get displayed.</TextBox>
</TabItem>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
我绑定的C#代码:
public IList<SharedProject> GetDummyData()
{
IList<SharedProject> projects = new List<SharedProject>();
SharedProject project1 = new SharedProject();
project1.Id = 1;
project1.ProjectName = "ProjectOne";
project1.ProjectDescription = "DescriptionOne";
project1.ProjectStartDate = DateTime.Now;
project1.ProjectEndDate = DateTime.Now.AddDays(30);
projects.Add(project1);
SharedProject project2 = new SharedProject();
project2.Id = 2;
project2.ProjectName = "ProjectTwo";
project2.ProjectDescription = "DesciptionTwo";
project2.ProjectStartDate = DateTime.Now;
project2.ProjectEndDate = DateTime.Now.AddDays(30);
projects.Add(project2);
return projects;
}
绑定项目和ProjectName有效,数据显示。 问题是,无论我有什么内容(在这个例子中只是一个TextBox),TabItem的内容都不会显示。 我注意到这个问题只有在我进行Binding时仍然存在。如果我从TabContem和TabItem的硬编码标题中删除绑定,一切正常。
答案 0 :(得分:0)
定义标题和内容的模板:
<强>更新强>
<TabControl Name="TabControl">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<TextBox Text="{Binding Content}"></TextBox>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Projects = new List<Test>();
Projects.Add(new Test("Tab1", "Content1"));
Projects.Add(new Test("Tab2", "Content2"));
Projects.Add(new Test("Tab3", "Content3"));
TabControl.ItemsSource = Projects;
}
public List<Test> Projects
{
get;
set;
}
}
public class Test
{
public Test(string name, string content)
{
this.Name = name;
this.Content = content;
}
public string Name { get; set; }
public string Content { get; set; }
}