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
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();DataContext = new MainWindowModel();}}/// <summary>/// Main window model/// </summary>public class MainWindowModel:NotifyPropertyChanged{private ObservableCollection<DeviceModel> devices;public ObservableCollection<DeviceModel> Devices{get { return devices; }set { devices = value; }}public MainWindowModel(){Devices = new ObservableCollection<DeviceModel>();for (int i = 0; i < 5; i++){Devices.Add(new DeviceModel() { Id = i + 1, Name = "设备" + (i + 1).ToString() });}}}/// <summary>/// Device data model/// </summary>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(); }}}/// <summary>/// Notifies clients that a property value has changed./// </summary>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
<Window x:Class="Deamon.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Deamon"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><TabControl ItemsSource="{Binding Devices}"><TabControl.ItemTemplate><DataTemplate><ContentPresenter Content="{Binding Name}"/></DataTemplate></TabControl.ItemTemplate><TabControl.ContentTemplate><DataTemplate><Grid><DockPanel><StackPanel><WrapPanel><TextBlock Text="编号:"/><TextBlock Text="{Binding Id}"/></WrapPanel><WrapPanel><TextBlock Text="名称:"/><TextBlock Text="{Binding Name}"/></WrapPanel></StackPanel><Polyline Points="15,16 95,110 154,300 125,10 541,95 15,16" Fill="#659BBC12" Stroke="#95100241" StrokeThickness="1"/></DockPanel></Grid></DataTemplate></TabControl.ContentTemplate></TabControl></Grid>
</Window>
控件样式设计跳转到我上一篇博客
积跬步以至千里:) (:一阵没来由的风