Jeevan Bhatt
Jeevan Bhatt

Reputation: 6101

WPF ListView control

How to add hyperlink column in wpf listview at runtime ?

Upvotes: 0

Views: 736

Answers (2)

Jeevan Bhatt
Jeevan Bhatt

Reputation: 6101

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;

Upvotes: 0

Timores
Timores

Reputation: 14589

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.

Upvotes: 2

Related Questions