TabControl

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(1,"zhangsan","110","bj");
            g1StuList.Add(s1);
            StudentGroup g1 = new StudentGroup(1,"一班","学习好",g1StuList);

            ObservableCollection<Student> g2StuList = new ObservableCollection<Student>();
            Student s2 = new Student(2, "lisi", "120", "bj");
            Student s3 = new Student(3, "wanger", "130", "bj");
            Student s4 = new Student(4, "maqi", "140", "bj");
            Student s5 = new Student(5, "gouba", "150", "bj");
            g2StuList.Add(s2);
            g2StuList.Add(s3);
            g2StuList.Add(s4);
            g2StuList.Add(s5);
            StudentGroup g2 = new StudentGroup(2, "二班", "爱玩", g2StuList);

            ObservableCollection<Student> g3StuList = new ObservableCollection<Student>();
            Student s6 = new Student(1, "zhangsan", "183", "shanghai");
            Student s7 = new Student(1, "zhangsan", "185", "nanjing");
            g3StuList.Add(s6);
            g3StuList.Add(s7);
            StudentGroup g3 = new StudentGroup(3, "三班", "体育好", g3StuList);

            GroupList.Add(g1);
            GroupList.Add(g2);
            GroupList.Add(g3);

            //当前选中的TabControl 赋初值
            SelectedGroupItem = GroupList[1];

        }

    }
}

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

时间: 2024-11-06 06:27:03

TabControl的相关文章

WPF TabControl only load the selected TabItem

1.binding ItemsSource public class TabItemViewModel { public string Header { get; set; } public FrameworkElement Content { get; set; } } xmlns:sc="clr-namespace:System.Collections;assembly=mscorlib"<Window.Resources> <sc:ArrayList x:Key

WPF学习(三)--Menu和TabControl控件介绍

Menu Menu提供了菜单栏方式的多级菜单的管理和操作: 这里对Menu的样式不做任何的定制和管理 下面来对Menu进行测试: 将Menu添加到页面中 运行后,效果如下: 这里没有考虑界面效果和样式. TabControl TabControl是非常常见和有用的控件, 数据控件 DataGrid DataGrid 是WPF中最常用的列表数据显示控件,该控件功能强大,并且拥有强大的可定制性 上面给出的是dataGrid的基本属性,下面我们通过设置样式,来设置下DataGrid的基本样式: 关于触

WPF——菜单栏及TabControl

一.先造一个窗体,然后在窗体里面增加菜单栏及原始的TabControl选项卡 <Grid> <Menu> <MenuItem Header="文件" Click="MenuItem_Click_3"> <MenuItem Header="打开新窗口" Click="MenuItem_Click_1"></MenuItem> <MenuItem Header=&q

C#控件TabControl隐藏page

隐藏 这个需求其实就是TABCONTROL控件会有很多提前制作好的PAGE页面,每次软件启动不可能所有页面都显示出来,目前想了个比较简单的方法解决这个问题 首先定义一个List集合存储TABCONTROL中的tabpages集合 然后LOAD的时候将所有的PAGE的parent设置为NULL,这样就不显示了,还想显示的时候需要把对应的PAGE.parent=tabcontrol控件就行了 private List<TabPage> tempTabPages=null; ------------

C#利用tabControl控件实现多窗体嵌入及关闭

创建一个主窗体(Formmain).两个副窗体(Form1,Form2);在主窗体中分别添加一个menuStrip控件.tabControl控件,并在menu控件上添加一个主菜单和两个子菜单,如下图: 继而,选中tabControl控件属性修改 DrawMode = OwnerDrawFixed,再根据如下代码添加即可: public void Add_TabPage(string str, Form myForm) { if (tabControlCheckHave(this.MainTabC

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

效果如图: 虽然说是自适应可关闭的TabControl,但TabControl并不需要改动,不如叫自适应可关闭的TabItem. 大体思路:建一个用户控件,继承自TabItem,里面放个按钮,点击的时候在TabControl中移除自身.在添加,移除TabItem和TabControl尺寸变化时,通过Items的个数计算合适的Width. 新建用户控件 新建用户控件,并继承自TabItem,这样它就拥有TabItem所有的属性和事件.而这个功能不需要自定义依赖属性和事件.它的用法就和TabItem

C# 自定义重绘TabControl

using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Runtime.InteropServices; using System; using System.Drawing.Text; using System.ComponentModel; namespace ControlExs.ControlExs.CTabControl { public class C

TabControl 伸缩式菜单 仿照 uwp SplitView

留下备用笔记 之前用的Frame+Page的切换content<类似于一个contentControl 干多个事情>,但是发现页面content内容控件多的时候,每一次切换都有点卡,点击了半天才反应过来(每次切换都要重新加载控件,具体怎么解决不知道..) 所以就产生了用tabcontrol的想法,tabControl可以多个Content,就第一次切换的时候卡,第二次的时候OK的,很流畅,所以就想着改一下这个tabControl 效果就是这么效果 上面的部分用了一headerContent 附

WPF TabControl 隐藏标头

1. 将每个 TabItem 的 Visibility 属性设置为 Visibility.Collapsed 即可隐藏标头 <TabItem Visibility="Collapsed"> 2. 设置 TabControl 控件的 SelectedIndex 属性即可在标签页之间切换

TabControl选项卡

<Grid> <TabControl Name="tabControl1"> <TabItem Name="tabItem1"> <TabItem.Header> <Button Content="adsf"/> </TabItem.Header> <StackPanel> <TextBlock>TABCONTROL控?件t的Ì?简¨°单Ì£¤使º1用