WPF ListView控件

时间:2010-05-04 10:48:21

标签: wpf listview

如何在运行时在wpf listview中添加超链接列?

2 个答案:

答案 0 :(得分:2)

GridViewColumn需要一个模板,这在运行时不容易创建。

最简单的方法是在XAML中创建一个DataTemplate,它具有所需的控件(即HyperlinkButton)。然后实例化GridViewColumn,获取资源并将其设置为CellTemplate属性。最后,将此列添加到GridView

的列列表中

答案 1 :(得分:0)

非常感谢大家,但现在我得到了一个很好的解决方案......

GridView gridView = new GridView();

FrameworkElementFactory tbContent;
FrameworkElementFactory hl;
DataTemplate dTemp;
GridViewColumn gvc;
FrameworkElementFactory tb;

tbContent = new FrameworkElementFactory(typeof(TextBlock));
tbContent.SetBinding(TextBlock.TextProperty, new Binding(backCheckViewModel.responseDetails.Columns[index].ColumnName));
hl = new FrameworkElementFactory(typeof(Hyperlink));
hl.AddHandler(Hyperlink.ClickEvent, new RoutedEventHandler(hyperLinkClick));
hl.AppendChild(tbContent);
tb = new FrameworkElementFactory(typeof(TextBlock));
tb.AppendChild(hl);
dTemp = new DataTemplate();
dTemp.VisualTree = tb;
gvc = new GridViewColumn();
gvc.Header = backCheckViewModel.responseDetails.Columns[index].ColumnName;
gvc.CellTemplate = dTemp;
gridView.Columns.Add(gvc);

lstResponses.View = gridView;