0

Id like to convert the contents of a cell to a hyperlink if the string value starts with "http". It has to work dynamically and alot of the example if found hard code the column value. Ideally i would like to just use value converter to replace the contents of a cell with a hyperlinkbutton if the content starts with "http".

    <esri:FeatureDataGrid x:Name="QueryDetailsDataGrid" Grid.Row="0"  Margin="0"
                        AutoGenerateColumns="False" CanUserSortColumns="True" 
                        VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
                        HeadersVisibility="Column" HorizontalScrollBarVisibility="Auto" 
                        SelectionChanged="QueryDetailsDataGrid_SelectionChanged"
                        LoadingRow="QueryDetailsDataGrid_LoadingRow"
                        Foreground="White" RowBackground="#555555"
                        AlternatingRowBackground="Black"
                        RowStyle="{StaticResource DataGridRowStyle}" >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click" SourceObject="{Binding ElementName=btnSave}">
                <actions:ExportTable />
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <esri:FeatureDataGrid.Columns>
            <sdk:DataGridTemplateColumn>
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding , Converter={StaticResource LinkConverter}}" />
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>
        </esri:FeatureDataGrid.Columns>
    </esri:FeatureDataGrid>

Converter

Public Class LinkConverter
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        If value Is Nothing Then
            Return vbNull
            Exit Function
        End If
        Dim _value As String = TryCast(value, String)
        If _value Is Nothing Then
            Return value
        End If
        If _value.StartsWith("http") Then
            Dim hl As HyperlinkButton = New HyperlinkButton With {.NavigateUri = New Uri(_value)}
            Return hl
        Else
            Return value
        End If
    End Function

    Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Dim _value As String = TryCast(value, String)
        If _value.StartsWith("http") Then
            Dim hl As Hyperlink = New Hyperlink With {.NavigateUri = New Uri(_value)}
            Return hl
        Else

            Return value
        End If
    End Function
End Class

This doesn't work because because im trying to replace the text with a framework element. Kinda stumped about how to do it this way....

NotARobot
  • 978
  • 2
  • 14
  • 36

1 Answers1

1
  1. Put two controls in DataTemplate - TextBlock and Hiperlink.
  2. Create HttpVisibile converter which returns Visibility.Visible for texts starting with "http" and Visibility.Collapsed for other texts. Bind Visibility Property of Hiperlink to your text using this converter.
  3. Create HttpCollaped converter, which returns Visibility.Collapsed for texts starting with http. Bind Visibility Property of TextBlock to the text using this converter

with these 3 steps you will achieve that all texts starting with "http" will appear as hiperlink.

<DataTemplate>
  <TextBlock Visibility="{Binding, Converter={StaticResource HttpCollapsedConverter}}" />
  <Hiperlink Visibility="{Binding, Converter={StaticResource HttpVisibleConverter}}" />
</DataTemplate>

Bind Text property of each control as required for you case.

Andris
  • 1,262
  • 1
  • 15
  • 24