2009-02-26 26 views
23

Tôi có thể lấy dữ liệu vào TabControl của tôi nhưng các tiêu đề có khung xung quanh chúng và tôi không thể trượt từ tab này sang tab khác.Làm cách nào để liên kết bộ sưu tập Danh sách với các tiêu đề TabControl trong WPF?

Tôi đang làm gì sai với cú pháp ràng buộc XAML trên TabControl này?

XAML:

<StackPanel> 
    <TabControl x:Name="TheTabControl"> 
     <TabControl.ItemTemplate> 
      <DataTemplate> 
       <TabItem Header="{Binding LastName}"> 
        <StackPanel Margin="10" Orientation="Horizontal"> 
         <TextBlock Text="{Binding FirstName}"/> 
         <TextBlock Text=" "/> 
         <TextBlock Text="{Binding LastName}"/> 
        </StackPanel> 
       </TabItem> 
      </DataTemplate>     
     </TabControl.ItemTemplate> 
    </TabControl> 

    <TabControl> 
     <TabItem Header="Tab1"> 
      <TextBlock Text="This is a test of tab 1"/> 
     </TabItem> 
     <TabItem Header="Tab2"> 
      <TextBlock Text="This is a test of tab 2"/> 
     </TabItem> 
    </TabControl> 

</StackPanel> 

mã sau:

public partial class Window1 : Window 
{ 
    public Window1() 
    { 
     InitializeComponent(); 

     //create all 
     List<Customer> customers = new List<Customer>(); 
     customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 }); 
     customers.Add(new Customer { FirstName = "Jane", LastName = "Smith", NumberOfContracts = 23 }); 
     customers.Add(new Customer { FirstName = "John", LastName = "Tester", NumberOfContracts = 23 }); 

     //show 
     TheListBox.ItemsSource = customers; 

    } 
} 

public class Customer 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public int NumberOfContracts { get; set; } 
} 
+0

u may mắn ur sử dụng WPF ... Tôi có vấn đề tương tự với Silverlight vì họ không hỗ trợ chuyển đổi itemsource cho tabcontrol. –

+0

Xem câu trả lời của Christof (http://stackoverflow.com/questions/589802/how-can-i-bind-a-list-collection-to-tabcontrol-headers-in-wpf/3196668#3196668) để XAML thực hiện công việc này - bạn cần một khối TabControl.ContentTemplate cho nội dung vì TabControl.ItemTemplate chỉ dành cho phần tiêu đề của tab. –

Trả lời

6

chỉ ràng buộc Danh sách của bạn để TabControl của bạn như ItemsSource, ví dụ

<TabControl ItemsSource="{Binding Customers}"/> 

điều này sẽ cung cấp cho bạn tab cho từng đối tượng trong khách hàng.

+2

Cảm ơn, điều đó đã giúp tôi tiếp tục, nhưng bây giờ tôi không thể nhấp từ tab này sang tab khác và văn bản tiêu đề có khung xung quanh. Tôi đã đăng mã mới ở trên, những gì cần phải thay đổi để tôi chỉ có thể liên kết dữ liệu vào tiêu đề và nội dung của các tab? –

+1

Đây là vấn đề của tôi - mọi thứ hoạt động ngoại trừ không thể chọn các tab bằng cách nhấp vào văn bản tiêu đề, điều này làm cho tabcontrol gần như vô dụng. – LineloDude

46

đây ist những gì tôi sẽ làm gì

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     //create all 
     var customers = new List<Customer>{ 
      new Customer {FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23}, 
      new Customer {FirstName = "Jane", LastName = "Smith", NumberOfContracts = 23}, 
      new Customer {FirstName = "John", LastName = "Tester", NumberOfContracts = 23}}; 

     //show 
     TheTabControl.ItemsSource = customers; 
     TheTabControl.SelectedIndex = 0; 
    } 


public class Customer 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public int NumberOfContracts { get; set; } 
} 

Và ở phía bên XAML

<TabControl x:Name="TheTabControl">    
    <TabControl.ItemTemplate> 
     <DataTemplate>      
      <TextBlock>        
       <TextBlock Text="{Binding FirstName}"/> <TextBlock Text="{Binding LastName}"/> 
      </TextBlock>       
     </DataTemplate> 
    </TabControl.ItemTemplate> 
    <TabControl.ContentTemplate> 
     <DataTemplate> 
      <TextBlock>        
       This is <TextBlock Text="{Binding FirstName}"/> <TextBlock Text="{Binding LastName}"/> 
      </TextBlock> 
     </DataTemplate> 
    </TabControl.ContentTemplate> 
</TabControl> 
+0

THIS thực sự hoạt động. :) –

+6

Ah !, 'ContentTemplate'. Cảm ơn! –

Các vấn đề liên quan