当前位置:  开发笔记 > 编程语言 > 正文

如何将List集合绑定到WPF中的TabControl标头?

如何解决《如何将List集合绑定到WPF中的TabControl标头?》经验,为你挑选了3个好方法。

我可以将数据导入到我的TabControl中,但是标题周围有框架,我无法从标签到标签.

我在TabControl上使用XAML绑定语法做错了什么?

XAML:


    
        
            
                
                    
                        
                        
                        
                    
                
                            
        
    

    
        
            
        
        
            
        
    

  

代码背后:

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

        //create all
        List customers = new List();
        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; }
}

小智.. 50

这就是我要做的

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

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //create all 
        var customers = new List{
            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; }
}

而在XAML方面

            
    
                            
                                        
                 
                                    
        
    
    
        
                                        
                This is  
            
        
    

啊!,`ContentTemplate`.谢谢! (6认同)


Joachim Kers.. 7

只需将List作为ItemsSource绑定到TabControl,例如


这将为您提供客户中每个对象的选项卡.



1> 小智..:

这就是我要做的

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

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //create all 
        var customers = new List{
            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; }
}

而在XAML方面

            
    
                            
                                        
                 
                                    
        
    
    
        
                                        
                This is  
            
        
    


啊!,`ContentTemplate`.谢谢!

2> Joachim Kers..:

只需将List作为ItemsSource绑定到TabControl,例如


这将为您提供客户中每个对象的选项卡.


谢谢,这让我更进一步,但我现在无法从标签到标签点击,标题文本周围有框架.我发布了上面的新代码,需要更改什么,所以我可以将数据绑定到标题和标签的内容?

3> 小智..:

你的答案可以在这里找到.

http://www.codeplex.com/smartclient/Thread/View.aspx?ThreadId=31821

注意他如何设置ContentTemplate以及ItemTemplate ...你几乎拥有它!

推荐阅读
yzh148448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有