tags:

views:

117

answers:

2

How to add hyperlink column in wpf listview at runtime ?

A: 

The GridViewColumn needs a template, which is not easy to create at runtime.

The easiest way to do this is to create a DataTemplate in XAML, that has the needed controls (i.e. the HyperlinkButton). Then instantiate a GridViewColumn, get the resource and set it to the CellTemplate property. Finally add this column to the list of columns of the GridView.

Timores
I believe there is also a TemplateSelector property that can be used to determine what template to use.
Tigraine
Right, but the problem, as I see it, is to create a Template in code (possible, but hard), not to select it.
Timores
A: 

Thanks a lot to all of you, But now I got a good solution which is...

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;
Jeevan Bhatt