动态地将Datagrid单元格转换为超链接

时间:2012-04-10 21:08:26

标签: silverlight xaml datagrid ivalueconverter

如果字符串值以“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

这不起作用,因为我试图用框架元素替换文本。有点难以理解如何这样做....

1 个答案:

答案 0 :(得分:1)

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

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

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