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;
}
}
}

最新文章

  1. SQLite 预写式日志
  2. MySQL 忘记root密码解决办法
  3. SpringData —— HelloWorld
  4. 使用jmeter进行性能测试-Jmeter教程及技巧汇总 (转)
  5. (转)POJ题目分类
  6. ExtJs之Ext.getCmp
  7. 2877: [Noi2012]魔幻棋盘 - BZOJ
  8. DataGridView 添加行 分类: DataGridView 2014-12-07 08:49 263人阅读 评论(0) 收藏
  9. logstash tomcat catalina.out 告警
  10. Java设计模式偷跑系列(十二)组合模式建模和实现
  11. visual studio 辅助工具
  12. Django源码学习 了解render与render_to_response
  13. Android 高级控件(七)——RecyclerView的方方面面
  14. Element-ui表格选中回显
  15. [Ynoi2019模拟赛]Yuno loves sqrt technology III
  16. javascript小实例,在页面中输出当前客户端时间
  17. Django分页(二)
  18. .NET开源Protobuf-net组件修炼手册
  19. Python shutil 模块
  20. Python3 tkinter基础 Canvas background 创建白色的画布 create_line width 画宽的线

热门文章

  1. eclipse块编辑
  2. VLC-开源播放器编译
  3. Android 系统启动过程详解
  4. mininet命令
  5. Gson解析json数据的案例一
  6. VirtualBox安装部署的Ubuntu16.04的步骤
  7. [BZOJ1054][HAOI2008]移动玩具 bfs+hash
  8. POJ 2184 Cow Exhibition【01背包+负数(经典)】
  9. 【字符串】Your Ride Is Here
  10. [洛谷3796]【模板】AC自动机(加强版)