我有一个只有ListBox的XAML文件。我动态创建列并添加行。使用此代码:
ListBoxItem l1 = new ListBoxItem();
StackPanel s1 = new StackPanel();
s1.Orientation = Orientation.Horizontal;
ContentPresenter ch1 = new ContentPresenter();
ch1.Content = "ICR";
s1.Children.Add(ch1); //just an example I add more than 1 column
li.Content = s1;
listbox.items.add(l1);
哪个工作正常,但现在我想创建列标题并按列排序。我可以使用ListBox动态地执行此操作,还是我走错了路径?
答案 0 :(得分:2)
如果您需要多个列和列标题,请不要使用ListBox
...请改用ListView
或DataGrid
。
至于排序,您可能必须手动完成,因为您填写列表的方式。如果您使用数据绑定会更容易。当您开始使用绑定时,关于 WPF中的所有更容易......
答案 1 :(得分:1)