How to make some column's content in the datagrid to be hyperlink?
I am wondering if it is an easy task. I need to have some of the text inside the datagrid to be hyperlinks. I generated content with XMLDataProvider. Any ideas are highly appreciated.
My DataGrid looks below. I am working with WPF toolkit:
<DataGrid x:Name="MainGrid" GridLinesVisibility="Horizontal" AutoGenerateColumns="False" RowHeight="20" ItemsSource="{Binding XPath=/ServicesTiles/Servers}"
HeadersVisibility="None" Margin="10,15,0,0"
HorizontalGridLinesBrush="{x:Null}"
Bor开发者_运维问答derBrush="{x:Null}"
VerticalGridLinesBrush="{x:Null}"
Background="{x:Null}"
RowBackground="{x:Null}"
CellStyle="{DynamicResource DataGridCellStyle1}"
HorizontalAlignment="Left">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath=@Name}" Foreground="#FF6E6E6E" MinWidth="160" />
<DataGridTextColumn Binding="{Binding XPath=@Status}" Foreground="#FF6E6E6E" MinWidth="270"/>
<DataGridTextColumn Binding="{Binding XPath=@Name2}" Foreground="#FF6E6E6E" MinWidth="220"/>
<DataGridTextColumn Binding="{Binding XPath=@Status2}" Foreground="#FF6E6E6E" MinWidth="220"/>
</DataGrid.Columns>
</DataGrid>
The following DataGridColumn should do what you want (I have not tested, make a comment, if it does not work)
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock >
<Hyperlink>
<TextBlock Text="{Binding YourProperty}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Edit
as requested, here my proposition how to insert. Let me know if it works...
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath=@Name}" Foreground="#FF6E6E6E" MinWidth="160" />
<DataGridTextColumn Binding="{Binding XPath=@Status}" Foreground="#FF6E6E6E" MinWidth="270"/>
<DataGridTextColumn Binding="{Binding XPath=@Name2}" Foreground="#FF6E6E6E" MinWidth="220"/>
<DataGridTextColumn Binding="{Binding XPath=@Status2}" Foreground="#FF6E6E6E" MinWidth="220"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock >
<Hyperlink>
<TextBlock Text="{Binding XPath=@Name2}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
Make use of the Command
on the Hyperlink
. If you want to call back into the DataContext
of the DataGrid
you will need to make use of FindAncestor
and search for type DataGrid
.
<toolkit:DataGridTemplateColumn>
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink Command="{Binding DoStuff}"/>
</TextBlock >
</DataTemplate >
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
精彩评论