gpt4 book ai didi

c# - 更改 ListView 中列的显示顺序

转载 作者:行者123 更新时间:2023-12-03 10:52:03 27 4
gpt4 key购买 nike

我必须根据某些显示更改列表 View 列的显示顺序
从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: how to bind GridViewColumn to ViewModel-Collection?

在下面,我分享了我创建的示例代码。注意:为简单起见,我使用了后面的代码,但是可以将代码很好地移动到您的 View 模型中。

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; }
}
}

关于c# - 更改 ListView 中列的显示顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18095384/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com