这几天做项目,需要做个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. php对文件操作(读、写、)的基础知识(详细)

    文件位置如下图所示: 1.判断是文件还是目录 var_dump(filetype("./aa/bb/cc.txt")); 输出: string(4) "file" ...

  2. 第 12 章 MySQL 可扩展设计的基本原则

    前言: 随着信息量的飞速增加,硬件设备的发展已经慢慢的无法跟上应用系统对处理能力的要求了.此时,我们如何来解决系统对性能的要求?只有一个办法,那就是通过改造系统的架构体系,提升系统的扩展能力,通过组合 ...

  3. java集合(3)- Java中的equals和hashCode方法详解

    参考:http://blog.csdn.net/jiangwei0910410003/article/details/22739953 Java中的equals方法和hashCode方法是Object ...

  4. Oracle CDC简介及异步在线日志CDC部署示例

    摘要 最近由于工作需要,花时间研究了一下Oracle CDC功能和LogMiner工具,希望能找到一种稳定.高效的技术来实现Oracle增量数据抽取功能.以下是个人的部分学习总结和部署实践. 1. O ...

  5. (数字IC)低功耗设计入门(五)——RTL级低功耗设计(续)

    二.RTL级低功耗设计(续) 前面一篇博文我记录了操作数隔离等低功耗设计,这里就主要介绍一下使用门控时钟进行低功耗设计. (4)门控时钟 门控时钟在我的第一篇博客中有简单的描述,这里就进行比较详细的描 ...

  6. 使用Generator 自动生成 model mapper mapping 文件

    1.下载包 地址http://download.csdn.net/detail/u012909091/7206091 2.下载完成解压文件到任意目录 3.删除下mybatis-generator-co ...

  7. Spring学习(6)---Bean定义及作用域的例子

    (一)Bean的定义 先定义一个BeanAnnotation package com.mypackage; import org.springframework.stereotype.Componen ...

  8. java 多线程基础

    线程是进程内的执行单元,进程当中都有若干个线程. 通常主线程或进程是阻塞式的按顺序执行的,如果希望异步执行些子任务,而不要阻塞当前线程的执行,即需要创建子线程,子线程创建后主线程可以等待它们的结果,得 ...

  9. EJB基础知识

    本人博客文章网址:https://www.peretang.com/basic-knowledge-of-ejb/ 什么是EJB 可移植的, 可重用的, 可伸缩的业务应用程序的平台 为什么选择EJB ...

  10. JDBC加载数据库驱动的方式

    JDBC作为数据库访问的规范接口,其中只是定义一些接口.具体的实现是由各个数据库厂商来完成. 一.重要的接口: 1.public interface Driver 每个驱动程序类必须实现的接口.Jav ...