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
{
    /// <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>

在这里插入图片描述

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


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

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值