1. ItemsSource="{Binding GroupList}" SelectedItem="{Binding SelectedGroupItem,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"

ItemsSource:绑定的数据列表

SelectedItem:当前选中项

2.<TabControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock></TextBlock>
                        <TextBlock Text="{Binding GroupName}" Tag="{Binding GroupName}" MaxWidth="80" TextTrimming="WordEllipsis"></TextBlock>
                    </StackPanel>
                </DataTemplate>
    </TabControl.ItemTemplate>

这里的意思是在TabControl的标签上,做了一个TextBlock,数据绑定的是GroupName,班级名,级“一班”,“二班”,“三班”

3.TabControl中使用了ListBox,注意事项

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Net;
using System.IO;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Collections.Generic;
using Newtonsoft.Json;
using System;
using System.Runtime.InteropServices;
using System.Xml;
using MVVM.Model;
using System.Xml.Linq;
using System.Windows;
using MVVM.communication;
using System.Threading;
using Microsoft.Win32;
using MVVM.Service;
using MVVM.ftp;
using System.Linq;
using System.Collections.ObjectModel; namespace MVVM.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// <para>
/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
/// </para>
/// <para>
/// You can also use Blend to data bind with the tool's support.
/// </para>
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class MainViewModel : ViewModelBase
{
/// <summary>
/// TabControl 绑定的数据列表
/// </summary>
public ObservableCollection<StudentGroup> GroupList//ObservableCollection
{
get { return _groupList; }
set { Set(() => GroupList, ref _groupList, value); }
}
private ObservableCollection<StudentGroup> _groupList = new ObservableCollection<StudentGroup>(); /// <summary>
/// 当前选中的TabControl
/// </summary>
public StudentGroup SelectedGroupItem
{
get { return _selectedGroupItem; }
set { Set(() => SelectedGroupItem, ref _selectedGroupItem, value); }
}
private StudentGroup _selectedGroupItem; public MainViewModel()
{
//TabControl 绑定的数据列表 初始化
ObservableCollection<Student> g1StuList = new ObservableCollection<Student>();
Student s1 = new Student(,"zhangsan","","bj");
g1StuList.Add(s1);
StudentGroup g1 = new StudentGroup(,"一班","学习好",g1StuList); ObservableCollection<Student> g2StuList = new ObservableCollection<Student>();
Student s2 = new Student(, "lisi", "", "bj");
Student s3 = new Student(, "wanger", "", "bj");
Student s4 = new Student(, "maqi", "", "bj");
Student s5 = new Student(, "gouba", "", "bj");
g2StuList.Add(s2);
g2StuList.Add(s3);
g2StuList.Add(s4);
g2StuList.Add(s5);
StudentGroup g2 = new StudentGroup(, "二班", "爱玩", g2StuList); ObservableCollection<Student> g3StuList = new ObservableCollection<Student>();
Student s6 = new Student(, "zhangsan", "", "shanghai");
Student s7 = new Student(, "zhangsan", "", "nanjing");
g3StuList.Add(s6);
g3StuList.Add(s7);
StudentGroup g3 = new StudentGroup(, "三班", "体育好", g3StuList); GroupList.Add(g1);
GroupList.Add(g2);
GroupList.Add(g3); //当前选中的TabControl 赋初值
SelectedGroupItem = GroupList[]; } }
}
<Window x:Class="MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"
Title="MainWindow" Height="600" Width="700">
<Window.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"></Binding>
</Window.DataContext>
<Grid>
<Grid.Resources>
<Style x:Key="BackColor" TargetType="Rectangle">
<Setter Property="Fill" Value="Black"></Setter>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="*"/> </Grid.RowDefinitions> <TabControl ItemsSource="{Binding GroupList}" SelectedItem="{Binding SelectedGroupItem,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="0,10,0,54" Grid.RowSpan="4">
<!--<TabControl.Resources>
<Style></Style>
</TabControl.Resources>-->
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock></TextBlock>
<TextBlock Text="{Binding GroupName}" Tag="{Binding GroupName}" MaxWidth="80" TextTrimming="WordEllipsis"></TextBlock>
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<!--<DataTemplate.Resources>
<Style></Style>
</DataTemplate.Resources>-->
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<StackPanel>
<StackPanel>
<TextBlock Text="班级ID" Grid.Column="0" Grid.Row="0"></TextBlock>
<TextBlock Text="{Binding GroupID}" Grid.Column="1" Grid.Row="0"></TextBlock>
</StackPanel>
<StackPanel>
<TextBlock Text="班级名称" Grid.Column="0" Grid.Row="1"></TextBlock>
<TextBlock Text="{Binding GroupName}" Grid.Column="1" Grid.Row="1"></TextBlock>
</StackPanel>
<StackPanel>
<TextBlock Text="班级描述" Grid.Column="0" Grid.Row="2"></TextBlock>
<TextBlock Text="{Binding GroupDesc}" Grid.Column="1" Grid.Row="2"></TextBlock>
</StackPanel>
</StackPanel>
<!-- 横线-->
<Rectangle Grid.Row="1" Margin="5,0,30,5" Height="2" VerticalAlignment="Center" HorizontalAlignment="Stretch"></Rectangle>
<ScrollViewer Grid.Row="2" CanContentScroll="False" Focusable="False" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<ListBox ItemsSource="{Binding StuList}">
<!--<ListBox.Resources>
<Style></Style>
</ListBox.Resources>--> <ListBox.ItemTemplate>
<DataTemplate>
<Expander IsExpanded="True">
<Expander.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</Expander.HeaderTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.Column="0">
<TextBlock Text="ID:"></TextBlock>
</StackPanel>
<StackPanel Grid.Row="0" Grid.Column="1">
<TextBlock Text="{Binding ID}"></TextBlock>
</StackPanel>
<StackPanel Grid.Row="1" Grid.Column="0">
<TextBlock Text="电话:"></TextBlock>
</StackPanel>
<StackPanel Grid.Row="1" Grid.Column="1">
<TextBlock Text="{Binding Telephone}"></TextBlock>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="0">
<TextBlock Text="地址:"></TextBlock>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="1">
<TextBlock Text="{Binding Address}"></TextBlock>
</StackPanel>
</Grid>
</Expander>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox> </ScrollViewer>
</Grid> </DataTemplate>
</TabControl.ContentTemplate> </TabControl> <StackPanel Grid.Row="1"> <!--<Button Content="点击我" Command="{Binding ClickCommand}"></Button>-->
</StackPanel>
</Grid>
</Window>
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MVVM.Model
{
public class StudentGroup : ObservableObject
{
public int GroupID
{
get { return _groupID; }
set { Set(() => GroupID, ref _groupID, value); }
}
private int _groupID;
public String GroupName
{
get { return _groupName; }
set { Set(() => GroupName, ref _groupName, value); }
}
private String _groupName; public String GroupDesc
{
get { return _groupDesc; }
set { Set(() => GroupDesc, ref _groupDesc, value); }
}
private String _groupDesc; public ObservableCollection<Student> StuList
{
get { return _stuList; }
set { Set(() => StuList, ref _stuList, value); }
}
private ObservableCollection<Student> _stuList = new ObservableCollection<Student>(); public StudentGroup(int id, String groupName, String groupDesc, ObservableCollection<Student> stuList)
{
this.GroupID = id;
this.GroupName = groupName;
this.GroupDesc = groupDesc;
this.StuList = stuList;
} public StudentGroup ()
{ }
}
}
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MVVM.Model
{
public class Student : ObservableObject
{
public int ID
{
get { return _id; }
set { Set(() => ID, ref _id, value); }
}
private int _id;
public String Name
{
get { return _name; }
set { Set(() => Name, ref _name, value); }
}
private String _name;
public String Telephone
{
get { return _telephone; }
set { Set(() => Telephone, ref _telephone, value); }
}
private String _telephone;
public String Address
{
get { return _address; }
set { Set(() => Address, ref _address, value); }
}
private String _address; public Student(int id, String name, String tele, String address)
{
this.ID = id;
this.Name = name;
this.Telephone = tele;
this.Address = address;
}
}
}

TabControl的更多相关文章

  1. TabControl 伸缩式菜单 仿照 uwp SplitView

    留下备用笔记 之前用的Frame+Page的切换content<类似于一个contentControl 干多个事情>,但是发现页面content内容控件多的时候,每一次切换都有点卡,点击了 ...

  2. WinForm中重绘TabControl选项卡标题

    最近开发WinForm频繁使用了TabControl控件,这个控件的选项卡没有BackgroundImage这个属性,那么如何为其各个选项卡添加背景图片呢?(这里说的是每个TabPage的头部,也就是 ...

  3. [WPF系列]-基础系列 TabControl应用

    引言 Tabcontrol控件也是我们在项目中经常用到的一个控件,用它将相关的信息组织在一起分类显示. 简介     ========================================= ...

  4. WPF自适应可关闭的TabControl 类似浏览器的标签页

    效果如图: 虽然说是自适应可关闭的TabControl,但TabControl并不需要改动,不如叫自适应可关闭的TabItem. 大体思路:建一个用户控件,继承自TabItem,里面放个按钮,点击的时 ...

  5. [C#开发小技巧]解决WinForm控件TabControl闪烁问题

    在用C#开发WinForm程序时,常发现TabControl出现严重的闪烁问题,这主要是由于TabControl控件在实现时会绘制默认的窗口背景.其实以下一段简单的代码可以有效的缓解该问题的发生.这就 ...

  6. c# 如何隐藏TabControl控件的标签

    http://www.cnblogs.com/chenleiustc/archive/2009/11/25/1527813.html 方法一:将标签缩小到机会看不到:设置页面的大小模式会自动适合(会尽 ...

  7. DotNetBar TabControl的使用

    这个和系统的TabPage不同,一个TabPage分为了DevComponents.DotNetBar.TabItem,DevComponents.DotNetBar.TabControlPanel两 ...

  8. DataGridView in TabControl and CellValidating lead to problems

    I created a little form with a TabControl on it and a combobox. On the  first page i added a DataGri ...

  9. 对TabControl的简单优化

    之前由于忙于赶项目进度而忽视了软件的用户体验,界面挺难看,有一天看见组长优化了某个窗体,让人感觉完全不一样,我也不甘示弱,要把我的程序做顺眼一点才行.我的程序是一个以TabControl为主要容器的窗 ...

  10. TabControl 显示彩色的图示 (XE6 Firemonkey)

    提示:Delphi 10 Seattle 透过 TImageList 来指定图标,就能显示原来图标的颜色. 下列方法只适用于 XE6 XE6 Firemonkey 里的 TabControl 可以将切 ...

随机推荐

  1. 转 Scrapy笔记(5)- Item详解

    Item是保存结构数据的地方,Scrapy可以将解析结果以字典形式返回,但是Python中字典缺少结构,在大型爬虫系统中很不方便. Item提供了类字典的API,并且可以很方便的声明字段,很多Scra ...

  2. django视图重定向

    # 原创,转载请留言联系 当请求访问到某个视图时,我们想让它重定向到其他页面,应该怎么做呢? 1.HttpResponseRedirect 需求:当我们访问127.0.0.1/my_redirect时 ...

  3. 【bugfree】安装

    我用的是WIN8系统 首先要安装XAMPP,开始里面的Apache和MySQL服务. 在运行Apache服务时报错: ----------------------------------------- ...

  4. Selenium2+python自动化6-八种元素元素定位(Firebug和firepath)【转载】

    前言 自动化只要掌握四步操作:获取元素,操作元素,获取返回结果,断言(返回结果与期望结果是否一致),最后自动出测试报告.本篇主要讲如何用firefox辅助工具进行元素定位.元素定位在这四个环节中是至关 ...

  5. .net core 发布iis 错误

    点击iis功能,例如 点击log日志,提示xxx路径下的web.config错误 百度之后 安装NET Core Windows Server Hosting ->DotNetCore.2.0. ...

  6. Python与数据库[2] -> 关系对象映射/ORM[0] -> ORM 与 sqlalchemy 模块

    ORM 与 sqlalchemy 1 关于ORM / About ORM 1.1 ORM定义 / Definition of ORM ORM(Object Relational Mapping),即对 ...

  7. Guess Number Higher or Lower -- LeetCode

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  8. (转)Unity3D协同程序(Coroutine)

    一.什么是协同程序 协同程序,即在主程序运行时同时开启另一段逻辑处理,来协同当前程序的执行.换句话说,开启协同程序就是开启一个线程. 二.协同程序的开启与终止 在Unity3D中,使用MonoBeha ...

  9. 说一说ST表 讲一讲水题

    ST表 一.算法介绍 如何快速求解RMQ问题呢?暴力复杂度O(n),线段树复杂度O(n)~O(logn),要是数据规模达到10^7或者更高呢?我们需要一种可以做到O(1)查询的算法,这时就可以用到ST ...

  10. 用swift开发自己的MacOS锁屏软件(二)

    上一篇中尝试写了hello world,这一篇中,开始尝试锁屏功能 1.尝试查找swift有没有相关的函数,可以控制系统锁屏之类的,结果并没有找到 2.尝试查找cocoa有没有相关的接口,结果仍然没有 ...