在代码隐藏中,您可以创建CellTemplate
:
GridView myGridView = new GridView();
myGridView.AllowsColumnReorder = false;
ListView l1 = new ListView();
GridViewColumn gvc0 = new GridViewColumn();
gvc0.DisplayMemberBinding = new Binding("AA");
gvc0.Header = "A/A";
gvc0.Width = 30;
myGridView.Columns.Add(gvc0);
GridViewColumn gvc1 = new GridViewColumn();
gvc1.Header = "Description";
gvc1.Width = 300;
FrameworkElementFactory fef = new FrameworkElementFactory(typeof(TextBlock));
FrameworkElementFactory hyperlinkHolder = new FrameworkElementFactory(typeof(Hyperlink));
hyperlinkHolder.SetBinding(Hyperlink.NavigateUriProperty, new Binding("Description"));
hyperlinkHolder.AddHandler(Hyperlink.RequestNavigateEvent, new RequestNavigateEventHandler(Hyperlink_RequestNavigate));
FrameworkElementFactory fef2 = new FrameworkElementFactory(typeof(TextBlock));
Binding placeBinding = new Binding();
fef2.SetBinding(TextBlock.TextProperty, placeBinding);
placeBinding.Path = new PropertyPath("Description");
hyperlinkHolder.AppendChild(fef2);
fef.AppendChild(hyperlinkHolder);
var dataTemplate = new DataTemplate();
dataTemplate.VisualTree = fef;
dataTemplate.DataType = typeof(ListViewItem);
gvc1.CellTemplate = dataTemplate;
myGridView.Columns.Add(gvc1);
l1.View = myGridView;
事件处理程序:
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
您应该使用 XAML 来做到这一点,它比您在代码隐藏中定义的所有 GUI 的第一个解决方案更清楚。
<ListView Name="l1">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="A/A" Width="30" DisplayMemberBinding="{Binding AA}" />
<GridViewColumn Header="Description" Width="300">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink NavigateUri="{Binding Description}" RequestNavigate="Hyperlink_RequestNavigate">
<TextBlock Text="{Binding Description}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>