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
2010-05-04 12:12:51
I believe there is also a TemplateSelector property that can be used to determine what template to use.
Tigraine
2010-05-04 12:15:00
Right, but the problem, as I see it, is to create a Template in code (possible, but hard), not to select it.
Timores
2010-05-04 15:21:28
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
2010-05-11 06:58:36