这几天做项目,需要做个listview满足能够动态显示或隐藏某些列,由于自己是菜鸟水平,查了两天资料也没有想出解决办法。就在我山穷水尽的时候看到了Mgen的一篇博客,给了我很大启发,所以我也决定把自己做的一些东西给大家说说,希望能帮助像我一样的菜鸟!

我读了Mgen的博文(http://www.cnblogs.com/mgen/archive/2011/07/24/2115458.html),给我很大启发,但也发现有些缺陷。我感觉的缺陷列举如下:

1、控制隐藏显示的逻辑关系有问题,搞不好会抛异常。

2、你设置管理控制显隐的控件只能是继承于itemcontrol的控件,有一定的局限性(比如说我想做一组button按钮,每个button控制一列话,它就不能用了)

下面,我贴出自己的代码,然后分析我是怎样解决这些问题的。

 <Window x:Class="mgen_autocolumns.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:mgen_autocolumns"
Title="Mgen" Height="350" Width="525">
<DockPanel>
<!-- 已经有的ListView -->
<ListView Name="list" DockPanel.Dock="Top" >
<ListView.View>
<GridView loc:GridViewUtility.ColumnObjectCollection="{Binding Path=ColumnCollection}">
<GridViewColumn Header="姓名"
Width="100"
DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="年龄"
Width="50"
DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="分数"
Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ProgressBar Width="80" Height="10" Maximum="100" Value="{Binding Score}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView> <!-- 列管理代码 -->
<!-- loc命名空间是ColumnObject的CLR命名空间 -->
<ListBox ItemsSource="{Binding Path=ColumnCollection.GViewAllCollection}">
<!--<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Focusable" Value="False" />
</Style>
</ListBox.ItemContainerStyle>-->
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsVisable}"
Content="{Binding Path=Header}"
Margin="2"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
</Window>

XAML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls; namespace mgen_autocolumns
{
public class GridViewUtility : GridView
{
#region 附加属性
public static readonly DependencyProperty ColumnObjectCollectionProperty =
DependencyProperty.RegisterAttached("ColumnObjectCollection", typeof(ColumnObjectCollections), typeof(GridView),
new PropertyMetadata(GridViewUtility.OnColumnObjectCollectionChanged)); static void OnColumnObjectCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ColumnObjectCollections col = e.NewValue as ColumnObjectCollections;
if (col != null)
{
col.GViewCollection = ((GridView)d).Columns;
int index = ;
col.GViewAllCollection = new List<ColumnObject>();
foreach (GridViewColumn gc in col.GViewCollection)
{
col.GViewAllCollection.Add(new ColumnObject(index, true,gc, col));
index++;
} } } public static ColumnObjectCollections GetColumnObjectCollection(GridView gridview)
{
return (ColumnObjectCollections)gridview.GetValue(ColumnObjectCollectionProperty);
} public static void SetColumnObjectCollection(GridView gridew, ColumnObjectCollections collection)
{
gridew.SetValue(ColumnObjectCollectionProperty, collection);
} #endregion
}
public class ColumnObjectCollections
{
public GridViewColumnCollection GViewCollection
{
get;
set;
}
public List<ColumnObject> GViewAllCollection
{
get;
set;
}
public void SetColumnVisable(int index, bool isVisable)
{
if (index >= && index < GViewAllCollection.Count)
{
GViewAllCollection[index].IsVisable = isVisable;
}
} public bool IsColumnVisable(int index)
{
if (index < || index >= GViewAllCollection.Count)
{
return false;
}
return GViewAllCollection[index].IsVisable;
}
}
public class ColumnObject
{
private int index;
private ColumnObjectCollections col;
private GridViewColumn column;
private bool isVisable;
public bool IsVisable
{
get
{
return isVisable;
}
set
{
isVisable = value;
SetVisable(isVisable);
} }
public string Header
{
get
{
return this.column.Header.ToString();
}
}
public ColumnObject(int index, bool isVisable,GridViewColumn column, ColumnObjectCollections col)
{
this.index = index;
this.IsVisable = true;
this.col = col;
this.column = column;
}
private void SetVisable(bool isVisable)
{
if (isVisable)
{
if (!this.IsVisable)
{
int index = this.index;
this.col.GViewAllCollection[index].isVisable = true;
for (int i = index + ; i < this.col.GViewAllCollection.Count; i++)
{
if (this.col.GViewAllCollection[i].isVisable)
{
index = this.col.GViewAllCollection[i].index - ;
break;
}
}
this.col.GViewCollection.Insert(index, this.column);
}
}
else
{
this.col.GViewCollection.Remove(this.column);
}
}
}
}
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace mgen_autocolumns
{
public class Person
{
public ColumnObjectCollections ColumnCollection
{
get;
set;
}
public Person()
{
ColumnCollection = new ColumnObjectCollections();
}
public Person(string name, int age, int score)
{
Name = name;
Age = age;
Score = score;
}
public string Name { get; set; }
public int Age { get; set; }
public int Score { get; set; } public static Person[] Get()
{
return new Person[]
{
new Person("Zhang", , ),
new Person("Mgen",,),
new Person("Lee",,),
new Person("",,),
new Person("Gao",,),
new Person("Sun",,),
new Person("David",,)
};
}
}
}

Person Code

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace mgen_autocolumns
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
this.DataContext = new Person();
InitializeComponent();
list.ItemsSource = Person.Get(); } }
}

MainWindow Code

WPF:动态显示或隐藏Listview的某一列的更多相关文章

  1. ListView中动态显示和隐藏Header&Footer

    ListView的模板写法 ListView模板写法的完整代码: android代码优化----ListView中自定义adapter的封装(ListView的模板写法) 以后每写一个ListView ...

  2. DataTables学习:从最基本的入门静态页面,使用ajax调用Json本地数据源实现前端开发深入学习,根据后台数据接口替换掉本地的json本地数据,以及报错的处理地方,8个例子(显示行附加信息,回调使用api,动态显示和隐藏列...),详细教程

    一.DataTables  个人觉得学习一门新的插件或者技术时候,官方文档是最根本的,入门最快的地方,但是有时候看完官方文档,一步步的动手写例子,总会出现各种莫名其妙的错误,需要我们很好的进行研究出错 ...

  3. Android6.0 源码修改之屏蔽导航栏虚拟按键(Home和RecentAPP)/动态显示和隐藏NavigationBar

    场景分析, 为了完全实现沉浸式效果,在进入特定的app后可以将导航栏移除,当退出app后再次将导航栏恢复.(下面将采用发送广播的方式来移除和恢复导航栏) ps:不修改源码的情况下,简单的沉浸式效果实现 ...

  4. wpf XMAL中隐藏控件

    原文:wpf XMAL中隐藏控件 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/a771948524/article/details/9264569 ...

  5. [WPF疑难] 如何限定ListView列宽度

    原文:[WPF疑难] 如何限定ListView列宽度 [WPF疑难] 如何限定ListView列宽度                                            周银辉 今天 ...

  6. vue实现动态显示与隐藏底部导航的方法分析

    本文实例讲述了vue实现动态显示与隐藏底部导航的方法.分享给大家供大家参考,具体如下: 在日常项目中,总有几个页面是要用到底部导航的,总有那么些个页面,是不需要底部导航的,这里列举一下页面底部导航的显 ...

  7. WPF datagrid AutoGenerateColumns隐藏部分列

    原文:WPF datagrid AutoGenerateColumns隐藏部分列 <DataGrid x:Name="gridWC" ItemsSource="{B ...

  8. WPF——绑定数据库数据(Listview)

    一.首先先画一个窗体,放进一个Listview 然后给每列起好名字,并且绑定的数据是临时表的列名 二.造一个临时表用来存储数据,并且将扔进去的Listview绑定到这个临时表DataTable上面 p ...

  9. 【aardio】如何对listview中某一列,某一行的特定值进行修改?

    用表格创建数组来实现. import win.ui; /*DSG{{*/ var winform = ..win.form( bottom=399;parent=...;right=599;text= ...

随机推荐

  1. 【JAVAWEB学习笔记】17_jsp

    动态页面技术(JSP/EL/JSTL) 学习目标 案例:完成商品的列表的展示 一.JSP技术 1.jsp脚本和注释 jsp脚本: 1)<%java代码%> ----- 内部的java代码翻 ...

  2. 【踩坑】360安全浏览器“极速模式”和“兼容模式”,套路还是bug?

    分享踩坑点: 项目中需要兼容360安全浏览器,大家当然都希望用极速模式打开网站,但是发现总是被兼容模式打开 网址类似 aa.xx.dd.com 网上找了很多地方,有以下两种方法 1.<meta ...

  3. Linux services, runlevels, and rc.d scripts

    Reference: [1] https://www.linux.com/news/introduction-services-runlevels-and-rcd-scripts A Linux se ...

  4. 【2017-05-22】WebForm内置对象:Application和ViewState、Repeater的Command用法

    一.内置对象 1.Application 存贮在服务器端,占用服务器内存生命周期:永久 所有人访问的都是这一个对象 传值:传的是object类型可以传对象. string s =TextBox1.Te ...

  5. RavenDB FS 安装使用 介绍

    前言 最近项目因为要存储图片和文件,折腾了RavenDB,使用RavenDB的FS系统统一管理图片和文件. 安装 RavenDB 的FS文件系统,需要用到windows的远程差分压缩功能: 安装好之后 ...

  6. 《算法4》2.1 - 插入排序算法(Insertion Sort), Python实现

    排序算法列表电梯: 选择排序算法:详见 Selection Sort 插入排序算法(Insertion Sort):非常适用于小数组和部分排序好的数组,是应用比较多的算法.详见本文 插入排序算法的语言 ...

  7. 全景技术大揭秘,市场核心早洞悉——VR全景加盟

    未来已来,未来已见.2017是3D全景创业的天时,全景行业逐步走向成熟.全景智慧城市专注vr全景6年,技术国内遥遥领先.全景智慧城市市场总监常诚,透漏3D全景技术和市场的核心. 拍摄全景必备的设备:单 ...

  8. python——爬虫&问题解决&思考(1)

    最近刚接触python,找点小任务来练练手,希望自己在实践中不断的锻炼自己解决问题的能力.这个小爬虫来自慕课网的一门课程,我在这里记录的是自己学习的过程中遇到的问题和解决方法以及爬虫之外的思考. 这次 ...

  9. Web缓存相关知识整理

    一.前言  工作上遇到一个这样的需求,一个H5页面在APP端,如果勾选已读状态,则下次打开该链接,会跳过此页面.用到了HTML5 的本地存储 API 中的 localStorage作为解决方案,回顾了 ...

  10. SQL Server AG集群启动不起来的临时自救大招

    SQL Server AG集群启动不起来的临时自救大招 背景 前晚一朋友遇到AG集群发生来回切换不稳定的情况,情急之下,朋友在命令行使用命令重启WSFC集群 结果重启WSFC集群之后,非但没有好转,导 ...