更改列表视图中列的显示顺序
本文关键字:显示 顺序 列表 视图 | 更新日期: 2023-09-27 18:26:50
我必须根据某些显示更改列表视图列的显示顺序从xml文件设置。有没有一种方法可以让我只在xaml中实现这一点,而不需要任何代码。我正在使用MVVM。
这是代码,我想根据从xml中读取的设置更改列的顺序(基本上是一系列列名)。
<ListView ItemsSource="{Binding MyList}" >
<ListView.View >
<GridView>
<GridViewColumn Header="column1" />
<GridViewColumn Header="column2"/>
<GridViewColumn Header="column3" />
<GridViewColumn Header="column4" />
<GridViewColumn Header="column5" />
</GridView>
</ListView.View>
</ListView>
这可以通过MVVM的方式完成。由于GridView.Columns
不是依赖属性,因此您必须定义自己的附加属性。请参阅此问题以定义附加属性。
WPF MVVM:如何将GridViewColumn绑定到ViewModel集合?
下面我分享了我创建的示例代码。注意:为了简单起见,我使用了后面的代码,但代码可以很好地移动到您的视图模型中。
GridViewColumns.cs-附加属性
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.Specialized;
using System.Reflection;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
public static class GridViewColumns
{
private static NotifyCollectionChangedEventHandler _CollectionChangedEventHandler = null;
[AttachedPropertyBrowsableForType(typeof(GridView))]
public static object GetColumnsSource(DependencyObject obj)
{
return (object)obj.GetValue(ColumnsSourceProperty);
}
public static void SetColumnsSource(DependencyObject obj, object value)
{
obj.SetValue(ColumnsSourceProperty, value);
}
// Using a DependencyProperty as the backing store for ColumnsSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ColumnsSourceProperty =
DependencyProperty.RegisterAttached(
"ColumnsSource",
typeof(object),
typeof(GridViewColumns),
new UIPropertyMetadata(
null,
ColumnsSourceChanged));
[AttachedPropertyBrowsableForType(typeof(GridView))]
public static string GetHeaderTextMember(DependencyObject obj)
{
return (string)obj.GetValue(HeaderTextMemberProperty);
}
public static void SetHeaderTextMember(DependencyObject obj, string value)
{
obj.SetValue(HeaderTextMemberProperty, value);
}
// Using a DependencyProperty as the backing store for HeaderTextMember. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderTextMemberProperty =
DependencyProperty.RegisterAttached("HeaderTextMember", typeof(string), typeof(GridViewColumns), new UIPropertyMetadata(null));
[AttachedPropertyBrowsableForType(typeof(GridView))]
public static string GetDisplayMemberMember(DependencyObject obj)
{
return (string)obj.GetValue(DisplayMemberMemberProperty);
}
public static void SetDisplayMemberMember(DependencyObject obj, string value)
{
obj.SetValue(DisplayMemberMemberProperty, value);
}
// Using a DependencyProperty as the backing store for DisplayMember. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisplayMemberMemberProperty =
DependencyProperty.RegisterAttached("DisplayMemberMember", typeof(string), typeof(GridViewColumns), new UIPropertyMetadata(null));
private static void ColumnsSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
GridView gridView = obj as GridView;
if (gridView != null)
{
gridView.Columns.Clear();
if (e.OldValue != null)
{
ICollectionView view = CollectionViewSource.GetDefaultView(e.OldValue);
if (view != null)
RemoveHandlers(gridView, view);
}
if (e.NewValue != null)
{
ICollectionView view = CollectionViewSource.GetDefaultView(e.NewValue);
if (view != null)
{
AddHandlers(gridView, view);
CreateColumns(gridView, view);
}
}
}
}
private static IDictionary<ICollectionView, List<GridView>> _gridViewsByColumnsSource =
new Dictionary<ICollectionView, List<GridView>>();
private static List<GridView> GetGridViewsForColumnSource(ICollectionView columnSource)
{
List<GridView> gridViews;
if (!_gridViewsByColumnsSource.TryGetValue(columnSource, out gridViews))
{
gridViews = new List<GridView>();
_gridViewsByColumnsSource.Add(columnSource, gridViews);
}
return gridViews;
}
private static void AddHandlers(GridView gridView, ICollectionView view)
{
GetGridViewsForColumnSource(view).Add(gridView);
view.CollectionChanged += ColumnsSource_CollectionChanged;
_CollectionChangedEventHandler = delegate(object sender, NotifyCollectionChangedEventArgs e)
{
var source = view.SourceCollection as ObservableCollection<ColumnDescriptor>;
if (source != null && e.Action == NotifyCollectionChangedAction.Move)
source.Move(e.OldStartingIndex, e.NewStartingIndex);
};
gridView.Columns.CollectionChanged += _CollectionChangedEventHandler;
}
private static void CreateColumns(GridView gridView, ICollectionView view)
{
foreach (var item in view)
{
GridViewColumn column = CreateColumn(gridView, item);
gridView.Columns.Add(column);
}
}
private static void RemoveHandlers(GridView gridView, ICollectionView view)
{
view.CollectionChanged -= ColumnsSource_CollectionChanged;
GetGridViewsForColumnSource(view).Remove(gridView);
gridView.Columns.CollectionChanged -= _CollectionChangedEventHandler;
}
private static void ColumnsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
ICollectionView view = sender as ICollectionView;
var gridViews = GetGridViewsForColumnSource(view);
if (gridViews == null || gridViews.Count == 0)
return;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var gridView in gridViews)
{
for (int i = 0; i < e.NewItems.Count; i++)
{
GridViewColumn column = CreateColumn(gridView, e.NewItems[i]);
gridView.Columns.Insert(e.NewStartingIndex + i, column);
}
}
break;
case NotifyCollectionChangedAction.Move:
foreach (var gridView in gridViews)
{
List<GridViewColumn> columns = new List<GridViewColumn>();
for (int i = 0; i < e.OldItems.Count; i++)
{
GridViewColumn column = gridView.Columns[e.OldStartingIndex + i];
columns.Add(column);
}
for (int i = 0; i < e.NewItems.Count; i++)
{
GridViewColumn column = columns[i];
// Check if column exists
if (gridView.Columns.Contains(columns[i]) == false)
gridView.Columns.Insert(e.NewStartingIndex + i, column);
}
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (var gridView in gridViews)
{
for (int i = 0; i < e.OldItems.Count; i++)
{
gridView.Columns.RemoveAt(e.OldStartingIndex);
}
}
break;
case NotifyCollectionChangedAction.Replace:
foreach (var gridView in gridViews)
{
for (int i = 0; i < e.NewItems.Count; i++)
{
GridViewColumn column = CreateColumn(gridView, e.NewItems[i]);
gridView.Columns[e.NewStartingIndex + i] = column;
}
}
break;
case NotifyCollectionChangedAction.Reset:
foreach (var gridView in gridViews)
{
gridView.Columns.Clear();
CreateColumns(gridView, sender as ICollectionView);
}
break;
default:
break;
}
}
private static GridViewColumn CreateColumn(GridView gridView, object columnSource)
{
GridViewColumn column = new GridViewColumn();
string headerTextMember = GetHeaderTextMember(gridView);
string displayMemberMember = GetDisplayMemberMember(gridView);
if (!string.IsNullOrEmpty(headerTextMember))
{
column.Header = GetPropertyValue(columnSource, headerTextMember);
}
if (!string.IsNullOrEmpty(displayMemberMember))
{
string propertyName = GetPropertyValue(columnSource, displayMemberMember) as string;
column.DisplayMemberBinding = new Binding(propertyName);
}
return column;
}
private static object GetPropertyValue(object obj, string propertyName)
{
if (obj != null)
{
PropertyInfo prop = obj.GetType().GetProperty(propertyName);
if (prop != null)
return prop.GetValue(obj, null);
}
return null;
}
}
}
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Content="Change Order" Click="Button_Click" Width="120" Margin="5"/>
<ListView Grid.Row="1" ItemsSource="{Binding MyList}" >
<ListView.View >
<GridView local:GridViewColumns.HeaderTextMember="HeaderText"
local:GridViewColumns.DisplayMemberMember="DisplayMember"
local:GridViewColumns.ColumnsSource="{Binding OrderedColumns}">
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
代码隐藏:
using System.Collections.Generic;
using System.Windows;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ViewModel _ViewModel = null;
public MainWindow()
{
InitializeComponent();
_ViewModel = new ViewModel();
this.DataContext = _ViewModel;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Moves the first column to the last
var temp = _ViewModel.OrderedColumns[0];
_ViewModel.OrderedColumns.Remove(temp);
_ViewModel.OrderedColumns.Add(temp);
}
}
public class ViewModel
{
private ObservableCollection<ColumnDescriptor> _OrderedColumns;
public ObservableCollection<ColumnDescriptor> OrderedColumns
{
get { return _OrderedColumns; }
set { _OrderedColumns = value; }
}
private List<Customer> _MyList;
public List<Customer> MyList
{
get { return _MyList; }
set { _MyList = value; }
}
public ViewModel()
{
OrderedColumns = new ObservableCollection<ColumnDescriptor>();
OrderedColumns.Add(new ColumnDescriptor() { HeaderText = "Column1", DisplayMember = "Column1" });
OrderedColumns.Add(new ColumnDescriptor() { HeaderText = "Column2", DisplayMember = "Column2" });
OrderedColumns.Add(new ColumnDescriptor() { HeaderText = "Column3", DisplayMember = "Column3" });
OrderedColumns.Add(new ColumnDescriptor() { HeaderText = "Column4", DisplayMember = "Column4" });
MyList = new List<Customer>();
MyList.Add(new Customer() { Column1 = "Data_Col1", Column2 = "Data_Col2", Column3 = "Data_Col3", Column4 = "Data_Col4" });
MyList.Add(new Customer() { Column1 = "2Data_Col1", Column2 = "2Data_Col2", Column3 = "2Data_Col3", Column4 = "2Data_Col4" });
}
}
public class ColumnDescriptor
{
public string HeaderText { get; set; }
public string DisplayMember { get; set; }
}
public class Customer
{
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; }
public string Column4 { get; set; }
}
}
为什么没有代码隐藏?关注点的分离是好的,但如果你使用后面的代码纯粹是为了设计你的视图,我认为这仍然是可以辩护的。