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= ...
随机推荐
- 【JAVAWEB学习笔记】25_Linux基础
Linux基础 学习目标 1.了解Linux的简介与安装 2.掌握Linux常用的命令 3.掌握Linux系统上JDK.Mysql.Tomcat的安装 一.Linux的简介 1.Linux的概述 Li ...
- 如何用C#寻找100到999的所有水仙花数?
首先解释一下何为水仙花数:水仙花数只是自幂数的一种,严格来说是三位数的个位.十位.百位的3次幂数相加等于原来的数字,称为水仙花数.(例如:1^3 + 5^3+ 3^3 = 153) 那么如何通过C#语 ...
- 《Android进阶》Sqlite的使用
之前认为Sqlite只能一次性创建多个表,其实不是 关键是对Sqlite的操作需要一些技巧: package com.example.mydemo; import android.content.Co ...
- Adobe Fireworks CS6 Mac破解版
Mac下一款快速建站的软件--Adobe Fireworks CS6,小子这里有时间就分享出来给更多需要的朋友. Adobe Fireworks CS6能让您在弹指间创作精美的网站和移动应用程序设计, ...
- springboot redis 缓存对象
只要加入spring-boot-starter-data-redis , springboot 会自动识别并使用redis作为缓存容器,使用方式如下 gradle加入依赖 compile(" ...
- VR全景智慧城市——商家的需求才是全景市场的核心竞争力
消费者视角痛点:比如酒店消费行业,很多消费者在预订酒店过程中,都遇到过这样的场景:网上照片里酒店房间看着宽敞明亮,格调不凡,感觉非常喜欢,等真正推开房门插上房卡一看,却大失所望.在酒店行业,网上照片和 ...
- 游戏UI框架设计(五): 配置管理与应用
游戏UI框架设计(五) --配置管理与应用 在开发企业级游戏/VR/AR产品时候,我们总是希望可以总结出一些通用的技术体系,框架结构等,为简化我们的开发起到"四两拨千金"的作用.所 ...
- JavaFx TableView疑难详解
TableView是个十分有用的控件,适应性和灵活性非常强,可以对它进行任意的修改,比如界面样式.功能.本文将从一步步提问的方式讲解TableView 创建已知列的TableView 已知列的表格的创 ...
- win7热点设置
1.设置热点名称与密码 netsh wlan set hostednetwork mode=allow ssid=costa key=11112222pause 2.开启 netsh wlan sta ...
- Vue单文件组件基础模板
背景 相信大家在使用Vue开发项目时,基本都是以单文件组件的形式开发组件的,这种方式好处多多: 1.代码集中,便于开发.管理和维护 2.可复用性高,直接将vue文件拷贝到新项目中 我暂时就想到这两点, ...