[英]Wpf binding columns to list
I'm wondering what is the best approach to my problem. 我想知道什么是解决我问题的最佳方法。 I need to generate DataGrid for my data. 我需要为我的数据生成DataGrid。 My data looks like this: 我的数据如下所示:
Competitor{
public string Name {get; set;}
public string Sname {get; set;}
public List<TimeSpan> Laps {get; set;}
}
Name and SName are known from the beggining. 从一开始就知道Name和SName。 When competitors makes circle he gets his Lap TimeSpan. 当竞争对手转圈时,他获得了单圈时间跨度。
So, after 5 laps DataGrid should look like this: 因此,经过5圈,DataGrid应该如下所示:
Name | SName | Lap1 | Lap2 | Lap3 | Lap4 | Lap5
asd | dsa | 1:1:1 | 1:2:1 | 1:0:1 | 1:2:2 | 1:3:1
bbb | cccc | 2:2:2 | 2:1:1 | 1:0:0 | 2:0:0 | 1:2:3
...
I checked this approach http://blogs.msmvps.com/deborahk/populating-a-datagrid-with-dynamic-columns-in-a-silverlight-application-using-mvvm/ but it's not exacly what I've expected. 我在http://blogs.msmvps.com/deborahk/populating-a-datagrid-with-dynamic-columns-in-a-silverlight-application-using-mvvm/中检查了这种方法,但这并不完全符合我的预期。
Any idea how to make this done? 任何想法如何做到这一点? (any other then manually adding column with new lap, it should be automaticly added) (然后再用新的圈数手动添加列的任何其他列,都应自动添加)
If you can use ListBox instead of DataGrid. 如果可以使用ListBox代替DataGrid。 You can do this: 你可以这样做:
**.xaml **。xaml
<StackPanel>
<!--Header-->
<StackPanel Orientation="Horizontal" Margin="5 0 0 0">
<TextBlock Width="100" Text="Name"/>
<TextBlock Width="100" Text="Sname"/>
<ListBox x:Name="ListHeader" BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Width="100"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</StackPanel>
<!--List of Competitors-->
<ListBox x:Name="Mylist">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="100" Text="{Binding Name}"/>
<TextBlock Width="100" Text="{Binding Sname}"/>
<ListBox ItemsSource="{Binding Laps}" BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Width="100"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
**.cs **。cs
public MainWindow()
{
InitializeComponent();
List<Competitor> list = new List<Competitor>();
list.Add(new Competitor { Name = "Renee", Sname = "Lewallen", Laps = new List<TimeSpan> { TimeSpan.Parse("00:10"), TimeSpan.Parse("00:20"), TimeSpan.Parse("00:30") } });
list.Add(new Competitor { Name = "Barney", Sname = "Fett", Laps = new List<TimeSpan> { TimeSpan.Parse("00:10"), TimeSpan.Parse("00:20"), TimeSpan.Parse("00:30"), TimeSpan.Parse("00:40") } });
list.Add(new Competitor { Name = "Nelle", Sname = "Butterfield", Laps = new List<TimeSpan> { TimeSpan.Parse("00:10"), TimeSpan.Parse("00:20"), TimeSpan.Parse("00:30"), TimeSpan.Parse("00:40"), TimeSpan.Parse("00:50") } });
list.Add(new Competitor { Name = "Marc", Sname = "Soriano", Laps = new List<TimeSpan> { TimeSpan.Parse("00:10"), TimeSpan.Parse("00:20") } });
list.Add(new Competitor { Name = "Cathi", Sname = "Stumpff", Laps = new List<TimeSpan> { TimeSpan.Parse("00:10"), TimeSpan.Parse("00:20"), TimeSpan.Parse("00:30") } });
list.Add(new Competitor { Name = "Jefferey", Sname = "Hunziker", Laps = new List<TimeSpan> { TimeSpan.Parse("00:10"), TimeSpan.Parse("00:20"), TimeSpan.Parse("00:30") } });
list.Add(new Competitor { Name = "Berniece", Sname = "Courtney", Laps = new List<TimeSpan> { TimeSpan.Parse("00:10"), TimeSpan.Parse("00:20"), TimeSpan.Parse("00:30") } });
var LapsCounter = list.Select(w => w.Laps.Count).Max();
List<string> listH = new List<string>();
for (int i = 1; i <= LapsCounter; i++)
{
listH.Add("Lap" + i);
}
Mylist.ItemsSource = list;
ListHeader.ItemsSource = listH;
}
public class Competitor
{
public string Name { get; set; }
public string Sname { get; set; }
public List<TimeSpan> Laps { get; set; }
}
Keep a DataTable
for your records. 保留一个DataTable
作为记录。 Bind this DataTable
with DataGrid's
AutogenerateColumns
to true
. 将此DataTable
与DataGrid's
AutogenerateColumns
绑定为true
。
Change List<Timespan>
to ObservableCollection<Timespan>
for Laps. 将Laps的List<Timespan>
更改为ObservableCollection<Timespan>
。 ObservableCollection
fires CollectionChanged
event when an item is added/removed. 添加/删除项目时, ObservableCollection
会引发CollectionChanged
事件。
Handle CollectionChanged
event and update DataTable
with adding more columns. 处理CollectionChanged
事件并通过添加更多列来更新DataTable
。
Now, we need to refresh the DataGrid
otherwise it won't show extra columns added. 现在,我们需要刷新DataGrid
否则它不会显示添加的额外列。 It will show the new row added but extra columns won't be shown. 它将显示添加的新行,但不会显示额外的列。 So, do this : 所以,这样做:
Dgrid1.ItemsSource = null; Dgrid1.ItemsSource = oldDataTable.DefaultView;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.