这几天做项目,需要做个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