2010-05-04 71 views

回答

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; 
2

GridViewColumn需要一個模板,在運行時不容易創建。

最簡單的方法是在XAML中創建一個DataTemplate,它具有所需的控件(即HyperlinkButton)。然後實例化一個GridViewColumn,獲取資源並將其設置爲CellTemplate屬性。最後將此列添加到GridView的列表列表中。

+0

我相信也是可以被用來確定要使用的模板TemplateSelector屬性。 – Tigraine 2010-05-04 12:15:00

+0

沒錯,但我認爲這個問題是在代碼中創建一個模板(可能,但很難),而不是選擇它。 – Timores 2010-05-04 15:21:28