2013-07-30 22 views
6

Đây là lớp học của tôi:Làm thế nào để thiết kế một TabPage khi ItemsSource của TabControl là Bind vào một danh sách trong WPF?

class mainViewModel 
    {  
     public List<Foo> F { get; set; } 
     public mainViewModel() 
     { 
     F=new List<Foo>() 
       { 
        new Foo(new Animal(){Name = "Cat"}), 
        new Foo(new Animal(){Name = "Dog"}), 
        new Foo(new Animal(){Name = "Camel"}) 
       }; 
     } 
    } 

    public class Foo 
    { 
     public Animal Animal { get; set; } 
     public Foo(Animal animal) 
     { 
      Animal = animal; 
     } 
    } 

    public class Animal 
    { 
     public string Name { get; set; } 
    } 

Và Đây là MainWindow XAML Mã của tôi:

<TabControl ItemsSource="{Binding Path=F}"> 
      <TabControl.ItemTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding Animal.Name}"/> 
       </DataTemplate> 
      </TabControl.ItemTemplate> 
      <TabControl.ContentTemplate> 
       <DataTemplate> 
        <TextBlock Text="Something 1"/> 
       </DataTemplate> 
      </TabControl.ContentTemplate> 
    </TabControl> 

Bây giờ rõ ràng là tôi sẽ có một TabControl với một trang cho mỗi mục trong danh sách F và tất cả các trang TabControl có một số TextBlockĐiều gì đó 1 như được hiển thị ở đây:

enter image description here

những gì tôi muốn chỉ là thiết kế một trong các trang. nói thêm button mới vào trang Camel có tên Điều gì đó 3.

+1

Tạo một ViewModel cụ thể cho từng và sử dụng 'DataTemplates' dựa trên từng loại ViewModel. Xem [ví dụ của tôi] (http://stackoverflow.com/a/15910760/643085) –

+0

Chỉ cần biết nếu tôi hiểu, dòng này [''] cho 'WPF' cho tất cả các TabPages thuộc loại T (ở đây FileSection) DataTemplate sẽ như sau. và tôi phải thêm các điều khiển của tôi bên dưới dòng này. Tôi có đúng không? –

+0

Đúng vậy. Sau đó, bạn tạo một Chế độ xem cụ thể cho mỗi. –

Trả lời

6

Theo các ý kiến ​​trên:

Tạo một lớp ViewModel cụ thể cho từng Tab:

public class Tab1: ViewModelBase 
{ 
    //... functionality, properties, etc 
} 

public class Tab2: ViewModelBase 
{ 
    //... functionality, properties, etc  
} 

public class Tab3: ViewModelBase 
{ 
    //... functionality, properties, etc  
} 

Sau đó Tạo một Xem cụ thể (thường là dưới hình thức UserControls) cho mỗi:

<UserControl x:Class"UserControl1" ...> 
    <!-- UI Elements, etc --> 
</UserControl> 

<UserControl x:Class"UserControl2" ...> 
    <!-- UI Elements, etc --> 
</UserControl> 

<UserControl x:Class"UserControl3" ...> 
    <!-- UI Elements, etc --> 
</UserControl> 

Sau đó, tạo DataTemplates cho từng loại ViewModel và đặt các UserControls này vào bên trong:

Edit: Những nên được quy định tại App.xaml dưới Application.Resources:

<Application ....> 
    <Application.Resources> 
     <DataTemplate DataType="{x:Type local:ViewModel1}"> 
      <local:UserControl1/> 
     </DataTemplate> 

     <DataTemplate DataType="{x:Type local:ViewModel2}"> 
      <local:UserControl2/> 
     </DataTemplate> 

     <DataTemplate DataType="{x:Type local:ViewModel3}"> 
      <local:UserControl2/> 
     </DataTemplate> 
    </Application.Resources> 
</Application> 

Cuối cùng, chấm ObservableCollection<ViewModelBase> trong ViewModel chính của bạn và thêm các mục:

public ObservableCollection<ViewModelBase> Tabs {get;set;} //Representing each Tab Item 

public MainViewModel() //Constructor 
{ 
    Tabs = new ObservableCollection<ViewModelBase>(); 
    Tabs.Add(new ViewModel1()); 
    Tabs.Add(new ViewModel2()); 
    Tabs.Add(new ViewModel2()); 
} 
+0

Xin lỗi @HighCore nhưng tôi chưa quen với 'WPF'. Bạn có ý nghĩa gì khi 'tạo DataTemplates cho mỗi Loại ViewModel và đặt những UserControls này vào bên trong chúng'? làm thế nào tôi có thể tạo DataTemplates cho một ViewModel ?? Trên thực tế tôi không biết nơi tôi nên viết những dòng ' ' –

+0

@MehrdadKamelzadeh Xem chỉnh sửa của tôi. –

+2

Puff !!! Cuối cùng làm việc. Tôi đặt những dòng này ở khắp mọi nơi trong ứng dụng của tôi nhưng nó không hoạt động;) @HighCore bạn là một người tiết kiệm cuộc sống :) –

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