在我的View中,我在ViewModel中有一个绑定到CollectionView的ListView,例如:
<ListView ItemsSource="{Binding MyCollection}" IsSynchronizedWithCurrentItem="true">
<ListView.View>
<GridView>
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding Path=Title}"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}"/>
<GridViewColumn Header="Phone" DisplayMemberBinding="{Binding Path=Phone}"/>
<GridViewColumn Header="E-mail" DisplayMemberBinding="{Binding Path=EMail}"/>
</GridView>
</ListView.View>
</ListView>
现在这些GridViewColumns已修复,但我希望能够从ViewModel更改它们。我想我必须将GridViewColumn集合绑定到ViewModel中的某些东西,但是什么,以及如何?
ViewModel对WPF一无所知,所以我不知道如何在MVVM中实现这一点。
这里有什么帮助吗?
答案 0 :(得分:38)
Columns
属性不是依赖项属性,因此您无法绑定它。但是,可以创建一个可以绑定到ViewModel中的集合的附加属性。然后,此附加属性将为您创建列。
更新
好的,这是一个基本的实现......
附加属性
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace TestPadWPF
{
public static class GridViewColumns
{
[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;
}
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);
}
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];
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;
}
}
}
<强>视图模型强>
class PersonsViewModel
{
public PersonsViewModel()
{
this.Persons = new ObservableCollection<Person>
{
new Person
{
Name = "Doe",
FirstName = "John",
DateOfBirth = new DateTime(1981, 9, 12)
},
new Person
{
Name = "Black",
FirstName = "Jack",
DateOfBirth = new DateTime(1950, 1, 15)
},
new Person
{
Name = "Smith",
FirstName = "Jane",
DateOfBirth = new DateTime(1987, 7, 23)
}
};
this.Columns = new ObservableCollection<ColumnDescriptor>
{
new ColumnDescriptor { HeaderText = "Last name", DisplayMember = "Name" },
new ColumnDescriptor { HeaderText = "First name", DisplayMember = "FirstName" },
new ColumnDescriptor { HeaderText = "Date of birth", DisplayMember = "DateOfBirth" }
};
}
public ObservableCollection<Person> Persons { get; private set; }
public ObservableCollection<ColumnDescriptor> Columns { get; private set; }
private ICommand _addColumnCommand;
public ICommand AddColumnCommand
{
get
{
if (_addColumnCommand == null)
{
_addColumnCommand = new DelegateCommand<string>(
s =>
{
this.Columns.Add(new ColumnDescriptor { HeaderText = s, DisplayMember = s });
});
}
return _addColumnCommand;
}
}
private ICommand _removeColumnCommand;
public ICommand RemoveColumnCommand
{
get
{
if (_removeColumnCommand == null)
{
_removeColumnCommand = new DelegateCommand<string>(
s =>
{
this.Columns.Remove(this.Columns.FirstOrDefault(d => d.DisplayMember == s));
});
}
return _removeColumnCommand;
}
}
}
XAML:
<ListView ItemsSource="{Binding Persons}" Grid.Row="0">
<ListView.View>
<GridView local:GridViewColumns.HeaderTextMember="HeaderText"
local:GridViewColumns.DisplayMemberMember="DisplayMember"
local:GridViewColumns.ColumnsSource="{Binding Columns}"/>
</ListView.View>
</ListView>
请注意,实际上并不需要ColumnDescriptor
类,为了清楚起见,我只添加了它,但任何类型都会这样做(包括匿名类型)。您只需指定哪些属性包含标题文本并显示成员名称。
另外,请记住它尚未经过全面测试,因此可能会有一些问题需要解决......
答案 1 :(得分:2)
我认为这段代码会导致一些内存泄漏问题;正如你所描述的GridViewColumns类,你已经定义了一个静态字典“_gridViewsByColumnsSource”,其中包含gridviews及其列的源引用;所以这是对添加的gridviews和列源的强引用;因为这个字典是静态的,所以似乎有一个静态引用“指向”gridviews和列源数据,如果gridview定义的屏幕关闭,如果GC启动,则无法通过GC收集gridview;随着越来越多类似的屏幕打开,越来越多的网格视图及其列源数据无法收集,最终会有内存泄漏。