本文主要实现下图所示的应用场景:

对于Class1页,会显示用户的age和address属性,对于Class2页,会显示用户的age,address和sex属性。在左边的ListBox中选择对应的用户,右侧会显示其对应的属性信息。

xaml代码如下:

<Controls:MetroWindow x:Class="TabControlAndListBoxDemo.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:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:local="clr-namespace:TabControlAndListBoxDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TabControl TabStripPlacement="Top" Name="tabcontrol">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Path=SelectionChangedEvent}"
CommandParameter="{Binding ElementName=tabcontrol, Path=SelectedIndex}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<TabItem Header="Class1" >
<Grid Visibility="{Binding Path=Class1Flag}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40*"/>
<ColumnDefinition Width="50*"/>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding Path=Users_Class1}" Name="listbox1" Grid.Column="0" Grid.ColumnSpan="1">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Grid.Column="1">
<Canvas Height="30" Margin="20,20,0,0">
<Label Content="Age:" Canvas.Left="20"/>
<TextBox Canvas.Left="100" Width="100" Text="{Binding ElementName=listbox1, Path=SelectedItem.Age}"/>
</Canvas>
<Canvas Height="30" Margin="20,20,0,0">
<Label Content="Address:" Canvas.Left="20"/>
<TextBox Canvas.Left="100" Width="100" Text="{Binding ElementName=listbox1, Path=SelectedItem.Address}"/>
</Canvas>
</StackPanel>
</Grid>
</TabItem>
<TabItem Header="Class2">
<Grid Visibility="{Binding Path=Class2Flag}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40*"/>
<ColumnDefinition Width="60*"/>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding Path=Users_Class2}" Name="listbox2" Grid.Column="0">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Grid.Column="1" Visibility="{Binding Path=Class2Flag}">
<Canvas Height="30" Margin="20,20,0,0">
<Label Content="Age:" Canvas.Left="20"/>
<TextBox Canvas.Left="100" Width="100" Text="{Binding ElementName=listbox2, Path=SelectedItem.Age}"/>
</Canvas>
<Canvas Height="30" Margin="20,20,0,0">
<Label Content="Address:" Canvas.Left="20"/>
<TextBox Canvas.Left="100" Width="100" Text="{Binding ElementName=listbox2, Path=SelectedItem.Address}"/>
</Canvas>
<Canvas Height="30" Margin="20,20,0,0">
<Label Content="Sex:" Canvas.Left="20"/>
<TextBox Canvas.Left="100" Width="100" Text="{Binding ElementName=listbox2, Path=SelectedItem.Sex}"/>
</Canvas>
</StackPanel>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Controls:MetroWindow>

对应的ViewModel的代码如下:

namespace TabControlAndListBoxDemo
{
public class MainWindowViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; private List<User> class1 = new List<User>();
private List<User> class2 = new List<User>();
private string class1flag = "hidden";
private string class2flag = "hidden";
public List<User> Users_Class1
{
get { return class1; }
set
{
class1 = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Users_Class1"));
}
}
public List<User> Users_Class2
{
get { return class2; }
set
{
class2 = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Users_Class2"));
}
} public string Class1Flag
{
get { return class1flag; }
set
{
class1flag = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Class1Flag"));
}
}
public string Class2Flag
{
get { return class2flag; }
set
{
class2flag = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Class2Flag"));
}
} public DelegateCommand SelectionChangedEvent { get; } public MainWindowViewModel()
{
Initiate();
SelectionChangedEvent = new DelegateCommand(SelectionChangedEventHandler);
} private void Initiate()
{
Users_Class1.Add(new User() { Name = "Lily", Age = 20, Address = "China" });
Users_Class1.Add(new User() { Name = "Tom", Age = 20, Address = "US" });
Users_Class2.Add(new User() { Name = "Spencer", Age = 21, Address = "Japan",Sex="Female" });
Users_Class2.Add(new User() { Name = "Jack", Age = 21, Address = "UK", Sex = "Male" });
} private void SelectionChangedEventHandler(object sender, DelegateCommandEventArgs args)
{
int Index = Convert.ToInt16(args.Parameter);
if(Index == 0)
{
Class1Flag = "Visible";
Class2Flag = "Hidden";
}
if(Index == 1)
{
Class1Flag = "Hidden";
Class2Flag = "Visible";
}
}
}
}

WPF中TabControl控件和ListBox控件的简单应用(MVVM)的更多相关文章

  1. C# 在DataGridView中,点击单元格调出 TreeView控件 或 ListBox控件

    1.调出 TreeView控件 或  ListBox控件 private void deductGrid1_CellClick(object sender, DataGridViewCellEvent ...

  2. ComboxBox控件、checklistbox控件和listbox控件的组合开发

    第一步:先创建一个WinForm窗体应用程序,按照下图所示的进行布局. 第二步:为ComboxBox控件.checklistbox控件和listbox控件和button控件设置属性 第三步:在代码中的 ...

  3. WPF中在摄像头视频上叠加控件的解决方案

    一.视频呈现 前段时间,在一个wpf的项目中需要实时显示ip摄像头,对此的解决方案想必大家都应该知道很多.在winform中,我们可以将一个控件(一般用panel或者pictruebox)的句柄丢给摄 ...

  4. WPF中。。DataGrid 实现时间控件和下拉框控件

    DatePicker 和新的 DataGrid 行 用户与 DataGrid 中日期列的交互给我造成了很大的麻烦. 我通过将一个 Data Source 对象拖动到 WPF 窗口上,创建了一个 Dat ...

  5. WPF中查找指定类型的父控件

    /// <summary> /// 查找父控件 /// </summary> /// <typeparam name="T"></type ...

  6. C# WinForm 中Console 重定向输出到ListBox控件中显示

                        {              VoidAction action =              {                  lstBox.Items. ...

  7. WPF中实现多选ComboBox控件

    在WPF中实现带CheckBox的ComboBox控件,让ComboBox控件可以支持多选. 将ComboBox的ItemsSource属性Binding到一个Book的集合, public clas ...

  8. 【WPF学习】第二十三章 列表控件

    WPF提供了许多封装项的集合的控件,本章介绍简单的ListBox和ComboBox控件,后续哈会介绍更特殊的控件,如ListView.TreeView和ToolBar控件.所有这些控件都继承自Item ...

  9. WPF进阶技巧和实战03-控件(3-文本控件及列表控件)

    系列文章链接 WPF进阶技巧和实战01-小技巧 WPF进阶技巧和实战02-布局 WPF进阶技巧和实战03-控件(1-控件及内容控件) WPF进阶技巧和实战03-控件(2-特殊容器) WPF进阶技巧和实 ...

随机推荐

  1. Go 面向对象编程应用

    #### Go 面向对象编程应用前面学习了很多的基础知识,这一节来实际写一个小案例:涉及到的知识: 1. 数组的基本使用2. 结构体3. 切片 4. 方法5. 循环6. 函数返回值(命名返回值,普通返 ...

  2. python网络爬虫-数据储存(七)

    数据储存 主要介绍两种数据储存方法: 储存在文件中,包括text文件和csv文件 存储在数据库中,包括MySQL关系型数据库和mongoDB数据库 存储到txt title = "第一个文本 ...

  3. 服务器+nextcloud搭建自己的私有云盘

    简介 Nextcloud是一款开源免费的私有云存储网盘项目,可以让你快速便捷地搭建一套属于自己或团队的云同步网盘,从而实现跨平台跨设备文件同步.共享.版本控制.团队协作等功能.它的客户端覆盖了Wind ...

  4. C# 将OFD转为PDF

    OFD格式的文档是一种我国独有的国家标准版式的文档,在不同场景需求中,可以通过格式转换的方法将PDF转为OFD,或者将OFD转为PDF.本次内容,将通过C#程序介绍如何实现由OFD到PDF的转换,并附 ...

  5. new JSONObject 无异常卡顿【Maven+Idea 导包不更新的小坑】

    问题描述 今天在使用JSONObject过程中出现了一个非常不可思议的现象,我Junit测试没有问题,但是就是打开服务器运行的时候,结果就是出不来,经过多次测试发现代码竟然卡在了new JSONObj ...

  6. HTML元素的隐藏方式

    感谢原文作者:幼儿园中的小小白 原文链接:https://blog.csdn.net/weixin_43846130/article/details/95963426 一.元素的隐藏方式: 1.dis ...

  7. EPF:一种基于进化、协议感知和覆盖率引导的网络协议模糊测试框架

    本文系原创,转载请说明出处:from 信安科研人 目录 实验 工具的安装 1.安装AFL++ 2.安装epf 对IEC104协议库进行fuzz 实验准备 使用AFL++中的编译器插桩 开始fuzz 原 ...

  8. 已完成的python项目-环境离线部署

    python环境离线部署 当前生产环境中,有很多基于python开发的工具需要使用. 由于python工具往往涉及到很多依赖,在线状态下,可以通过pip requirements来管理安装. 但有时候 ...

  9. Pandas中Series与Dataframe的初始化

    (一)Series初始化 1.通过列表,index自动生成 se = pd.Series(['Tom', 'Nancy', 'Jack', 'Tony']) print(se) 2.通过列表,指定in ...

  10. uos系统安装tree

    apt install tree 提示无法安装软件包 执行apt update 然后执行apt install tree