WPF TabControl 数据绑定

WPF TabControl in Binding’s world

首先,TabControl是间接继承自ItemControl的控件,因此可以像ItemControl那样自如的使用。

自此,我们知道了ItemControl的派生控件有:

ItemControl–>Selector–>ListBox

ItemControl–>Selector–>ListBox–>ListView

ItemControl–>Selector–>ComboBox

ItemControl–>Selector–>TabControl

TabControl与ItemControl主要区别多了一个公共显示区域ContentTemplate,该区域可以显示当前选择项的一些特殊信息,因此,我们可以按照如下的形式进行开发。

简单版数据绑定使用

定义数据模型与数据绑定

MainWindow.xaml.cs

/*
 * function: TabControl in DataBinding's world
 * description: Using TabControl to Binding 
 * author: Mei Liyong
 */

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace Deamon
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new MainWindowModel();
        }
    }

    /// 
    /// Main window model
    /// 
    public class MainWindowModel:NotifyPropertyChanged
    {
        private ObservableCollection devices;

        public ObservableCollection Devices
        {
            get { return devices; }
            set { devices = value; }
        }

        public MainWindowModel()
        {
            Devices = new ObservableCollection();

            for (int i = 0; i < 5; i++)
            {
                Devices.Add(new DeviceModel() { Id = i + 1, Name = "设备" + (i + 1).ToString() });
            }
        }
    }

    /// 
    /// Device data model
    /// 
    public class DeviceModel: NotifyPropertyChanged
    {

        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; OnPropertyChanged(); }
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; OnPropertyChanged(); }
        }

    }

    /// 
    /// Notifies clients that a property value has changed.
    /// 
    public class NotifyPropertyChanged : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string PropertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }

        protected void OnPropertyChanged([CallerMemberName] string PropertyName = null)
        {
            RaisePropertyChanged(PropertyName);
        }
    }
}

完成数据绑定

MainWindow.xaml


    
        
            
                
                    
                
            
            
                
                    
                        
                            
                                
                                    
                                    

                                

                                
                                    
                                    

                                
                            

                            

                        
                    
                    
                
            
        
    


WPF TabControl 数据绑定_第1张图片

控件样式设计跳转到我上一篇博客


积跬步以至千里:) (:一阵没来由的风

你可能感兴趣的:(C#,WPF)