WPF TabControl 数据绑定

article/2025/10/10 23:23:27

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>

在这里插入图片描述

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


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


http://chatgpt.dhexx.cn/article/dFYwlMLn.shtml

相关文章

WPF TabControl Styles

WPF TabControl Styles 水平使用的TabControl 效果&#xff1a; 样式资源 <!-- 顶部TabControl控件样式 --><SolidColorBrush x:Key"TabItem.Static.Background" Color"White"/><SolidColorBrush x:Key"TabItem.Static.Border&quo…

MFC tabcontrol切换界面

1.添加控件tabcontrol。 2.切换到资源界面->Dialog->插入Dialog&#xff0c;创建两个Dialog界面。 3.为新建的两个Dialog添加类&#xff0c;在新建的Dialog界面右键类向导添加对应的类。 4.添加TabSheet.cpp 和TabSheet.h 这两个文件从网上下载即可&#xff0c;具体源码如…

TabControl控件

点餐用到的控件&#xff1a; 1&#xff09;TabControl: 管理并向用户显示可以包含控件和组件的相关选项卡的信息 2&#xff09;ComboBox: 显示一个可编辑的文本框&#xff0c;其中包含一个允许值下拉列表 3&#xff09;DateTimePicker: 允许用户设定日期和时间&#x…

WPF 控件专题 TabControl控件详解

1、TabControl 介绍 TabControl 表示包含多个项的控件&#xff0c;这些项共享屏幕上的同一空间&#xff0c;每个区域都可以通过单击通常位于控件顶部的选项卡标题来访问。 也叫选项卡控件。 *******************************************************************************…

WPF 基础控件之 TabControl样式

其他基础控件 1.Window2.Button3.CheckBox4.ComboBox5.DataGrid 6.DatePicker7.Expander8.GroupBox9.ListBox10.ListView11.Menu12.PasswordBox13.TextBox14.RadioButton15.ToggleButton16.Slider 17.TreeView TabControl 实现下面的效果 1&#xff09;TabControl来实现动画&…

WPF TabControl美化

实现效果 XMAL样式 <Window.Resources><!-- TabItem的样式 --><Style TargetType"{x:Type TabItem}"><Setter Property"Template"><Setter.Value><ControlTemplate TargetType"{x:Type TabItem}"><Gr…

WPF动态加载TabControl

主要讲一下如何通过点击菜单&#xff0c;实现动态加载TabControl的功能&#xff0c;准确来说应该是动态加载TabItem,要实现这个功能&#xff0c;我们需要解决两个问题&#xff1a; 点击菜单的时候&#xff0c;需要传进来一个你要加载的UserControl控件的名称&#xff0c;让程序…

C# TabControl

C# TabControl 你好&#xff01; 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章&#xff0c;了解一下Markdown的基本语法知识。 新的改变 我们对Markdown编辑器进行了一些功能拓展与语法支持&#xff0c;除了标…

TabControl

jquery主体&#xff1a; var TabControl function (ops) {this._ops {items: ops.items || [],hashItems: {},selectedIndex: ops.selectedIndex || 0};this._element $(ops.element);this._tabContainerId "ui-tabcontrol-container-";this._convertHashItems()…

WinForm TabControl美化

一、简述 TabControl控件是winform里非常常用的一个控件&#xff0c;但是默认的tab控件的标签页和文字颜色都是无法修改的。有时候我们会要用到竖排列的标签页&#xff0c;或者想要更改标签页的背景颜色和字体时&#xff0c;自带的TabControl就满足不了我们的需求了&#xff0…

MFC中选项卡TabControl控件的用法

前言&#xff1a;我这里的开发环境是VS2010&#xff0c;其它不同的开发环境可能会有所差别&#xff0c;但绝不会差太多&#xff0c;其根本方法一般是不会变的。 选项卡控件(英文名&#xff1a;TabControl),这个控件使用在开发一些比较复杂&#xff0c;和用户交互性比较多的软件…

C#中TabControl相关用法

最近在用C#做项目时&#xff0c;用到TabControl这个控件&#xff0c;将学到的东西做个总结&#xff1a; 一、拖一个TabControl控件到窗口上&#xff0c;在控件上点击右键&#xff0c;可以添加选项卡/删除选项卡&#xff1b;或者在属性中找到TabPages,点击进去&#xff0c;可以看…

【Wayland】Weston启动流程分析

Weston启动流程分析 Weston是Wayland Compositor的实现。其Server端作为独立的进程运行在系统中。MakeFile中编译成果为&#xff0c;“weston”的可执行程序MakeFile.am(weston 2.0.0) bin_PROGRAMS westonweston_LDFLAGS -export-dynamic weston_CPPFLAGS $(AM_CPPFLAGS)…

Weston 纹理倒置(render-gl)

纹理倒置 背景 在 render-gl 接入 frame buffer object 实现 off-screen 渲染后,发现得到的渲染图发生了180的倒置. 查阅了有关资料后,在 eglspec.1.5 中的 2.2.2.1 Native Surface Coordinate Systems 找到了答案: The coordinate system for native windows and pixmaps …

【Wayland】Wayland简介与定制指导

Wayland与Weston简介 由于某些原因。移植并定制一套基于Wayland的Compositor。Wayland与Weston&#xff0c;是两个相辅相成的概念。这里简单总结一下&#xff1a; wayland是一套为“显示”服务的协议&#xff0c;基于C/S结构。它定制了一套标准的接口、基本通信方式。wayland…

display:weston:weston-simple-egl

写在前面&#xff1a; 客户端渲染 在Wayland架构中&#xff0c;客户端UI的所有呈现均由客户端代码执行&#xff0c;通常由客户端使用的图形工具包执行。 图形工具箱可以使用其希望呈现UI元素的任何方法&#xff1a;在CPU上进行软件呈现&#xff0c;使用GLES进行硬件呈现。 W…

Ubuntu 20.04 X86成功编译运行wayland、wayland-protocols、weston,亲测有效,踩了很多坑,完美解决。

编译前期准备&#xff1a; 1、更换国内源&#xff1a; #添加阿里源 deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb http://mirrors.aliyun.c…

weston 窗口管理 (1)

窗口管理 (1) 一、概述 在传统嵌入式场景下,通常只会运行一个UI程序,故相当于单窗口程序,无需桌面服务器的介入;在桌面系统下,对于每一个UI程序而言,它们的行为相比于嵌入式场景仍然没有发生改变,其对接的仍然是窗口,只不过在同一个时刻允许多个UI程序同时运行. 无论如何对于…

weston 简介

参考???weston wiki Weston - Gentoo Wiki weston (1): Linux man pages – code.tools Weston-1.12.0 非常详尽&#xff0c;多图慎入&#xff1a;Wayland与Weston简介 - 云社区 - 腾讯云 什么是weston&#xff1f; Wayland是一套display server(Wayland compositor)与…

waylandweston

简单地说&#xff0c;Wayland是一套display server(Wayland compositor)与client间的通信协议&#xff0c;而Weston是Wayland compositor的参考实现。其官网为http://wayland.freedesktop.org/。它们定位于在Linux上替换X图形系统。X图形系统经历了30年左右的发展&#xff0c;其…