0

如果字符串值以“http”开头,我想将单元格的内容转换为超链接。如果发现对列值进行硬编码,它必须动态工作并且有很多示例。理想情况下,如果内容以“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>

转换器

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

这不起作用,因为我试图用框架元素替换文本。有点困惑如何做到这一点......

4

1 回答 1

1
  1. 在 DataTemplate 中放置两个控件 - TextBlock 和 Hiperlink。
  2. 创建 HttpVisibile 转换器,它为以“http”开头的文本返回 Visibility.Visible,为其他文本返回 Visibility.Collapsed。使用此转换器将 Hiperlink 的可见性属性绑定到您的文本。
  3. 创建 HttpCollaped 转换器,它为以 http 开头的文本返回 Visibility.Collapsed。使用此转换器将 TextBlock 的可见性属性绑定到文本

通过这 3 个步骤,您将实现所有以“http”开头的文本都将显示为超链接。

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

根据您的情况需要绑定每个控件的 Text 属性。

于 2012-04-11T05:56:19.617 回答