如何在运行时在wpf listview中添加超链接列?
答案 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;