コード ビハインドでは、以下を作成できます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>