我正在寻找一种将二维集合绑定到ListView
GridView
的方法。到目前为止,我已经能够将我的数据绑定到看起来像网格的嵌套ItemsControl
控件。但是我希望它的行为类似于GridView
,其中列可以由用户调整大小。
视图模型如下所示:
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
namespace BindingTest
{
public class ViewModel : ViewModelBase
{
public ObservableCollection<string> ColumnHeaders
{
get { return _columnHeaders; }
set
{
if (_columnHeaders == value)
return;
_columnHeaders = value;
RaisePropertyChanged(() => ColumnHeaders);
}
}
public ObservableCollection<ObservableCollection<object>> Data
{
get { return _data; }
set
{
if (_data == value)
return;
_data = value;
RaisePropertyChanged(() => Data);
}
}
private ObservableCollection<string> _columnHeaders;
private ObservableCollection<ObservableCollection<object>> _data;
public ViewModel()
{
ColumnHeaders = new ObservableCollection<string>();
ColumnHeaders.Add("String Column");
ColumnHeaders.Add("Boolean Column");
Data = new ObservableCollection<ObservableCollection<object>>();
for(int i = 0; i < 5; i++)
{
ObservableCollection<object> row = new ObservableCollection<object>();
row.Add("cell " + i);
row.Add(i % 2 == 0 ? true : false);
Data.Add(row);
}
}
}
}
Data
中的内部集合可以包含任何类型的对象,并且可以有任意数量的列。我还需要能够使用不同的DataTemplate
来呈现每个单元格,具体取决于对象类型。
我目前的观点xaml如下所示:
<UserControl x:Class="BindingTest.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:clr="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
DataContext="{Binding ViewModel, Source={StaticResource Locator}}">
<UserControl.Resources>
<DataTemplate x:Key="StringDataTemplate">
<TextBlock Text="{Binding}"/>
</DataTemplate>
</UserControl.Resources>
<StackPanel>
<ItemsControl ItemsSource="{Binding ColumnHeaders}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Width="100" Text="{Binding }"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl ItemsSource="{Binding Data}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel Orientation="Horizontal">
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding}">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type clr:String}">
<TextBox Width="100" Text="{Binding Path=.}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type clr:Boolean}">
<CheckBox Width="100" IsChecked="{Binding Path=.}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type clr:DateTime}">
<DatePicker Width="100" SelectedDate="{Binding Path=.}"/>
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</UserControl>
我想在GridView
上使用ListView
,但我无法弄清楚如何以MVVM方式绑定到我的数据。
答案 0 :(得分:0)
请尝试下一个解决方案。所有为我们提供解决方案的人here。所以我试图将你的问题与后面链接中的人的解决方案相匹配。
<强> XAML 强>
<UserControl x:Class="WPFAddGridViewColumnDynamicHelpAttempt.BindingTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:clr="clr-namespace:System;assembly=mscorlib"
xmlns:wpfAddGridViewColumnDynamicHelpAttempt="clr-namespace:WPFAddGridViewColumnDynamicHelpAttempt"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<wpfAddGridViewColumnDynamicHelpAttempt:ViewModel/>
</UserControl.DataContext>
<ListView x:Name="ListView" ItemsSource="{Binding Data}" Grid.Row="0">
<ListView.View>
<GridView wpfAddGridViewColumnDynamicHelpAttempt:GridViewColumns.HeaderTextMember="HeaderText"
wpfAddGridViewColumnDynamicHelpAttempt:GridViewColumns.Parent="{Binding ElementName=ListView}"
wpfAddGridViewColumnDynamicHelpAttempt:GridViewColumns.DisplayMemberMember="DisplayMember"
wpfAddGridViewColumnDynamicHelpAttempt:GridViewColumns.ColumnTemplateKey="ColumnTemplateTemplateKey"
wpfAddGridViewColumnDynamicHelpAttempt:GridViewColumns.ColumnsSource="{Binding ColumnHeaders}">
</GridView>
</ListView.View>
</ListView></UserControl>
ViewModel和模型(请参阅评论)
public class ViewModel : BaseObservableObject
{
public ObservableCollection<ColumnDescriptor> ColumnHeaders
{
get { return _columnHeaders; }
set
{
if (_columnHeaders == value)
return;
_columnHeaders = value;
OnPropertyChanged(() => ColumnHeaders);
}
}
public ObservableCollection<Person> Data
{
get { return _data; }
set
{
if (_data == value)
return;
_data = value;
OnPropertyChanged(() => Data);
}
}
//here I've changed the column object from string to ColumnDescriptor object,
//to match the values defined in AttachedPropeties (GridViewColumns class)
//please keep in mind that every colun is binded to data in model, thus if your model
//won't have property named Salary, but your ColumnHeaders collection will have the ColumnDescriptor
//with that DisplayMember you will have binding expression exception in output window.
//So you have define ColumnHeaders collection with certain number of ColumnDescriptors (as you want to display by model).
private ObservableCollection<ColumnDescriptor> _columnHeaders;
//since the Data is binded to the listview that give its ItemsSource to the GridView,
//we don't have to have an ObservableCollection of ObservableCollections of objects, we can provide the actual values and
//they will be distrebuted to presented columns
private ObservableCollection<Person> _data;
public ViewModel()
{
ColumnHeaders = new ObservableCollection<ColumnDescriptor>
{
new ColumnDescriptor {HeaderText = "Last name", DisplayMember = "LastName", ColumnTemplateTemplateKey = typeof(String)},
new ColumnDescriptor {HeaderText = "First name", DisplayMember = "FirstName", ColumnTemplateTemplateKey = typeof(String)},
new ColumnDescriptor {HeaderText = "Salary", DisplayMember = "Salary", ColumnTemplateTemplateKey = typeof(String)},
new ColumnDescriptor {HeaderText = "Date Of Birth", DisplayMember = "DateOfBirth", ColumnTemplateTemplateKey = typeof(DateTime)},
new ColumnDescriptor {HeaderText = "Gift Was Delivered", DisplayMember = "GiftWasDelivered", ColumnTemplateTemplateKey = typeof(Boolean)},
};
Data = new ObservableCollection<Person>();
for (int i = 0; i < 5; i++)
{
Data = new ObservableCollection<Person>
{
new Person {FirstName = "John", LastName = "A.", DateOfBirth=new DateTime(1965, 12, 1), GiftWasDelivered = false},
new Person {FirstName = "Ron", LastName = "B.", DateOfBirth=new DateTime(1995, 6, 8), GiftWasDelivered = false}
};
}
}
}
/// <summary>
/// defines the column data (column heder text and column content)
/// </summary>
public class ColumnDescriptor
{
/// <summary>
/// defines the actual text that will be shown in the grid view column header
/// </summary>
public string HeaderText { get; set; }
/// <summary>
/// defines the name of the property in view model that will be presented by this column
/// </summary>
public string DisplayMember { get; set; }
/// <summary>
/// defines the key of thew template
/// </summary>
public object ColumnTemplateTemplateKey { get; set; }
}
public class Person:BaseObservableObject
{
private string _name;
private string _lastName;
private DateTime _dateOfBirth;
private bool _giftWasDelivered;
public bool GiftWasDelivered
{
get { return _giftWasDelivered; }
set
{
_giftWasDelivered = value;
OnPropertyChanged();
}
}
public string FirstName
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged();
}
}
public string LastName
{
get { return _lastName; }
set
{
_lastName = value;
OnPropertyChanged();
}
}
public DateTime DateOfBirth
{
get { return _dateOfBirth; }
set
{
_dateOfBirth = value;
OnPropertyChanged();
}
}
}
}
从here改编附属财产,将其用作黑匣子解决方案
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>>();
[AttachedPropertyBrowsableForType(typeof(GridView))]
public static string GetColumnTemplateKey(DependencyObject element)
{
return (string)element.GetValue(ColumnTemplateKeyProperty);
}
public static void SetColumnTemplateKey(DependencyObject element, string value)
{
element.SetValue(ColumnTemplateKeyProperty, value);
}
public static readonly DependencyProperty ColumnTemplateKeyProperty =
DependencyProperty.RegisterAttached("ColumnTemplateKey", typeof (string), typeof (GridViewColumns),
new PropertyMetadata(default(string)));
public static object GetParent(DependencyObject element)
{
return (object)element.GetValue(ParentProperty);
}
public static void SetParent(DependencyObject element, object value)
{
element.SetValue(ParentProperty, value);
}
public static readonly DependencyProperty ParentProperty = DependencyProperty.RegisterAttached("Parent",
typeof (object), typeof (GridViewColumns), new PropertyMetadata(default(object)));
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;
}
}
//here we create columns and define theire binding,
//in addition we can create (or find) data template for each columns
private static GridViewColumn CreateColumn(GridView gridView, object columnSource)
{
GridViewColumn column = new GridViewColumn();
string headerTextMember = GetHeaderTextMember(gridView);
string displayMemberMember = GetDisplayMemberMember(gridView);
string templateKey = GetColumnTemplateKey(gridView);
if (!string.IsNullOrEmpty(headerTextMember))
{
column.Header = GetPropertyValue(columnSource, headerTextMember);
}
if (!string.IsNullOrEmpty(templateKey))
{
DefineSimpleCustomDataTemplateWithBinding(columnSource, templateKey, displayMemberMember, column);
}
else
{
DefineDefaultBinding(columnSource, displayMemberMember, column);
}
return column;
}
private static void DefineSimpleCustomDataTemplateWithBinding(object columnSource, string templateKey, string displayMemberMember,
GridViewColumn column)
{
var keyType = GetPropertyValue(columnSource, templateKey) as Type;
if (keyType == null)
{
DefineDefaultBinding(columnSource, displayMemberMember, column);
}
else
{
var propertyName = GetPropertyValue(columnSource, displayMemberMember) as string;
var binding = new Binding(propertyName) {FallbackValue = "data is not presented by provided model"};
var dataTemplate = DataTemplateFactory.GetDataTemplateWithBindingByType(keyType, binding);
if (dataTemplate == null)
{
DefineDefaultBinding(columnSource, displayMemberMember, column);
}
else
{
column.CellTemplate = dataTemplate;
}
}
}
private static void DefineDefaultBinding(object columnSource, string displayMemberMember, GridViewColumn column)
{
if (!string.IsNullOrEmpty(displayMemberMember))
{
string propertyName = GetPropertyValue(columnSource, displayMemberMember) as string;
var binding = new Binding(propertyName) {FallbackValue = "data is not presented by provided model"};
column.DisplayMemberBinding = binding;
}
}
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;
}
}
工厂在代码中创建数据模板(根据需要创建自己的模板)
/// <summary>
/// helps to define simple data template in code
/// </summary>
public class DataTemplateFactory
{
public static DataTemplate GetDataTemplateWithBindingByType(Type type, Binding binding)
{
var key = type.Name;
DataTemplate template;
switch (key)
{
case "String":
{
template = GetStringTemplate(binding);
}
break;
case "Boolean":
{
template = GetBooleanTemplate(binding);
}
break;
case "DateTime":
{
template = GetDateTimeTemplate(binding);
}
break;
default:
return null;
}
return template;
}
private static DataTemplate GetDateTimeTemplate(Binding binding)
{
DataTemplate template = new DataTemplate();
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(DatePicker));
factory.SetValue(Control.BackgroundProperty, Brushes.CadetBlue);
factory.SetBinding(DatePicker.SelectedDateProperty, binding);
template.VisualTree = factory;
return template;
}
private static DataTemplate GetBooleanTemplate(Binding binding)
{
DataTemplate template = new DataTemplate();
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(CheckBox));
factory.SetValue(ContentControl.ContentProperty, "");
factory.SetValue(Control.BackgroundProperty, Brushes.Green);
factory.SetBinding(ToggleButton.IsCheckedProperty, binding);
template.VisualTree = factory;
return template;
}
private static DataTemplate GetStringTemplate(Binding binding)
{
DataTemplate template = new DataTemplate();
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(TextBlock));
factory.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Left);
factory.SetValue(TextBlock.BackgroundProperty, Brushes.Tomato);
factory.SetBinding(TextBlock.TextProperty, binding);
template.VisualTree = factory;
return template;
}
}
更新(添加地图制作工具)
Xaml代码
<UserControl x:Class="WPFAddGridViewColumnDynamicHelpAttempt.BindingTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:clr="clr-namespace:System;assembly=mscorlib"
xmlns:wpfAddGridViewColumnDynamicHelpAttempt="clr-namespace:WPFAddGridViewColumnDynamicHelpAttempt"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" x:Name="This">
<UserControl.DataContext>
<wpfAddGridViewColumnDynamicHelpAttempt:UpdatedViewModel/>
</UserControl.DataContext>
<ListView x:Name="OuterListView" ItemsSource="{Binding Data}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<ListView x:Name="InnerListView" ItemsSource="{Binding MappedObjects}">
<ListView.View>
<GridView wpfAddGridViewColumnDynamicHelpAttempt:GridViewColumns.HeaderTextMember="HeaderText"
wpfAddGridViewColumnDynamicHelpAttempt:GridViewColumns.DisplayMemberMember="DisplayMember"
wpfAddGridViewColumnDynamicHelpAttempt:GridViewColumns.ColumnTemplateKey="ColumnTemplateTemplateKey"
wpfAddGridViewColumnDynamicHelpAttempt:GridViewColumns.ColumnsSource="{Binding ElementName=This, Path=DataContext.ColumnHeaders}">
</GridView>
</ListView.View>
</ListView>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView></UserControl>
ViewMOdel和mappers
public class UpdatedViewModel:BaseObservableObject
{
public ObservableCollection<ColumnDescriptor> ColumnHeaders
{
get { return _columnHeaders; }
set
{
if (_columnHeaders == value)
return;
_columnHeaders = value;
OnPropertyChanged(() => ColumnHeaders);
}
}
public ObservableCollection<MappedCollection<MappedObject>> Data
{
get { return _data; }
set
{
if (_data == value)
return;
_data = value;
OnPropertyChanged(() => Data);
}
}
private ObservableCollection<ColumnDescriptor> _columnHeaders;
private ObservableCollection<MappedCollection<MappedObject>> _data;
public UpdatedViewModel()
{
//ColumnHeaders = new ObservableCollection<string>();
//ColumnHeaders.Add("String Column");
//ColumnHeaders.Add("Boolean Column");
ColumnHeaders = new ObservableCollection<ColumnDescriptor>
{
new ColumnDescriptor {HeaderText = "String Column", DisplayMember = "StringColumn", ColumnTemplateTemplateKey = typeof(String)},
new ColumnDescriptor {HeaderText = "Bolean Column", DisplayMember = "BoleanColumn", ColumnTemplateTemplateKey = typeof(Boolean)},
};
Data = new ObservableCollection<MappedCollection<MappedObject>>();
var mappedCollection = new MappedCollection<MappedObject>();
for(int i = 0; i < 5; i++)
{
//row.Add("cell " + i);
//row.Add(i % 2 == 0 ? true : false);
mappedCollection.MappedObjects.Add(new MappedObject { StringColumn = string.Format("cell:{0}", i), BoleanColumn = i % 2 == 0 });
}
Data.Add(mappedCollection);
}
}
public class MappedCollection<T>:BaseObservableObject
{
private ObservableCollection<T> _mappedObjects;
public MappedCollection()
{
MappedObjects = new ObservableCollection<T>();
}
public ObservableCollection<T> MappedObjects
{
get { return _mappedObjects; }
set
{
_mappedObjects = value;
OnPropertyChanged();
}
}
}
public class MappedObject:BaseObservableObject
{
private object _stringColumn;
private object _boleanColumn;
public object StringColumn
{
get { return _stringColumn; }
set
{
_stringColumn = value;
OnPropertyChanged();
}
}
public object BoleanColumn
{
get { return _boleanColumn; }
set
{
_boleanColumn = value;
OnPropertyChanged();
}
}
}
如果代码出现问题,我很乐意提供帮助。
问候。