リストビューへの動的列を作成するために使用する追加のプロパティを持つリストビューがあります
リストビューへの動的列を作成するために使用する追加のプロパティを持つリストビューがあります
public class ListViewExtension
{
public static readonly DependencyProperty MatrixSourceProperty = DependencyProperty.RegisterAttached("MatrixSource", typeof(ObservableCollection<CountItem>), typeof(ListViewExtension), new FrameworkPropertyMetadata(null, OnMatrixSourceChanged));
public static IEnumerable<CountItem> GetMatrixSource(DependencyObject d)
{
return (ObservableCollection<CountItem>)d.GetValue(MatrixSourceProperty);
}
public static void SetMatrixSource(DependencyObject d, ObservableCollection<CountItem> value)
{
d.SetValue(MatrixSourceProperty, value);
}
private static void OnMatrixSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ListView listView = (ListView)d;
ObservableCollection<CountItem> items = (ObservableCollection<CountItem>)e.NewValue;
listView.ItemsSource = items;
GridView gridView = new GridView();
foreach (CountItem countItem in items)
{
gridView.Columns.Add(
new GridViewColumn
{
Header = "Date",
DisplayMemberBinding = new Binding("Date")
});
foreach (SubItem subItem in countItem.SubItems)
{
gridView.Columns.Add(
new GridViewColumn
{
Header = String.Format("{0} {1}", subItem.Firstname, subItem.Lastname),
DisplayMemberBinding = new Binding("AccountNumber")
});
}
}
listView.View = gridView;
}
}
<ListView MoneyCounter:ListViewExtension.MatrixSource="{Binding CountItems}" />
リストは、CountItemのObserverableCollectionで構成されるCountItemsにバインドされています。CountItemは、DateとSubItemの別のObserverableCollectionを持つ単純なクラスであり、Firstname、Lastname、およびAccountnumberが含まれています。
私のコードでは、最初の列は(正しくバインドされる)日付である必要がありますが、次の列はSubItemを持つN列です。
しかし、どうすればこれらのアイテムをバインドできますか?
回答: いじくり回した後、私は次のようになりました。
for (Int32 i = 0; i < countItem.SubItem.Count; i++)
{
SubItem subItem = countItem.DenominationItems[i];
gridView.Columns.Add(
new GridViewColumn
{
Header = String.Format("{0} {1}", subItem.Firstname, subItem.Lastname),
DisplayMemberBinding = new Binding("SubItems["+i+"].AccountNumber");
});
}