WPF:动态显示或隐藏Listview的某一列
这几天做项目,需要做个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的某一列的更多相关文章
- ListView中动态显示和隐藏Header&Footer
ListView的模板写法 ListView模板写法的完整代码: android代码优化----ListView中自定义adapter的封装(ListView的模板写法) 以后每写一个ListView ...
- DataTables学习:从最基本的入门静态页面,使用ajax调用Json本地数据源实现前端开发深入学习,根据后台数据接口替换掉本地的json本地数据,以及报错的处理地方,8个例子(显示行附加信息,回调使用api,动态显示和隐藏列...),详细教程
一.DataTables 个人觉得学习一门新的插件或者技术时候,官方文档是最根本的,入门最快的地方,但是有时候看完官方文档,一步步的动手写例子,总会出现各种莫名其妙的错误,需要我们很好的进行研究出错 ...
- Android6.0 源码修改之屏蔽导航栏虚拟按键(Home和RecentAPP)/动态显示和隐藏NavigationBar
场景分析, 为了完全实现沉浸式效果,在进入特定的app后可以将导航栏移除,当退出app后再次将导航栏恢复.(下面将采用发送广播的方式来移除和恢复导航栏) ps:不修改源码的情况下,简单的沉浸式效果实现 ...
- wpf XMAL中隐藏控件
原文:wpf XMAL中隐藏控件 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/a771948524/article/details/9264569 ...
- [WPF疑难] 如何限定ListView列宽度
原文:[WPF疑难] 如何限定ListView列宽度 [WPF疑难] 如何限定ListView列宽度 周银辉 今天 ...
- vue实现动态显示与隐藏底部导航的方法分析
本文实例讲述了vue实现动态显示与隐藏底部导航的方法.分享给大家供大家参考,具体如下: 在日常项目中,总有几个页面是要用到底部导航的,总有那么些个页面,是不需要底部导航的,这里列举一下页面底部导航的显 ...
- WPF datagrid AutoGenerateColumns隐藏部分列
原文:WPF datagrid AutoGenerateColumns隐藏部分列 <DataGrid x:Name="gridWC" ItemsSource="{B ...
- WPF——绑定数据库数据(Listview)
一.首先先画一个窗体,放进一个Listview 然后给每列起好名字,并且绑定的数据是临时表的列名 二.造一个临时表用来存储数据,并且将扔进去的Listview绑定到这个临时表DataTable上面 p ...
- 【aardio】如何对listview中某一列,某一行的特定值进行修改?
用表格创建数组来实现. import win.ui; /*DSG{{*/ var winform = ..win.form( bottom=399;parent=...;right=599;text= ...
随机推荐
- Error:Android Source Generator: [sdk] Android SDK is not specified.
有时候使用intellij idea 带入android 项目,运行提示Error:Android Source Generator: [sdk] Android SDK is not specifi ...
- 开涛spring3(6.9) - 对JDBC的支持 之 7.1 概述
7.1 概述 7.1.1 JDBC回顾 传统应用程序开发中,进行JDBC编程是相当痛苦的,如下所示: //cn.javass.spring.chapter7. TraditionalJdbcTes ...
- source install sshpass in aix
1.源码下载: wget https://nchc.dl.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz 2.解压 ...
- css如何让div和页面等高?
我们都知道,只要是block状态的标签,宽度和父级等宽,或者设置宽度100%也可以等宽,但设置高度100%是不管用的,那么如何让标签和页面等高呢,除了用js去动态计算设置高度值,用css也可以 只要将 ...
- dotnet 命令实战
以下用实例串起dotnet所有命令,带你玩转dotnet命令. 1.创建(dotnet new) 首先我们创建一个项目,这里我们创建控制台程序,命令如下图所示. dotnet new dotnet n ...
- JAVA设计模式初探之装饰者模式
定义:动态给一个对象添加一些额外的职责,就象在墙上刷油漆.使用Decorator模式相比用生成子类方式达到功能的扩充显得更为灵活.设计初衷:通常可以使用继承来实现功能的拓展,如果这些需要拓展的功能的种 ...
- cef3和duilib简单仿有道词典学习
由于最近换工作的原因,也没啥事,就简单学习了一下cef3和duilib,楼主之前是做MFC框架下的windows开发的,对界面库和新的客户端开发模式也有所了解,现在的大部分客户端都是基本的客户端框架下 ...
- JavaScript 特效三大系列总结
一. offset系列 1. offset系列的5个属性 1. offsetLeft : 用于获取元素到最近的定位父盒子的左侧距离 * 计算方式: 当前元素的左边框的左侧到定位父盒子的左边框右侧 * ...
- (转)对Http协议的长连接和短连接新的认识
转载来自:http://www.cnblogs.com/zuoxiaolong/p/life49.html一直对长连接短连接模模糊糊,看着该博主的文章,豁然开朗~ 引言 最近刚到公司不到一个月,正处于 ...
- Myeclipse快捷键组合
------------------------------------- MyEclipse 快捷键1(CTRL) ------------------------------------- Ctr ...