Select to view content in your preferred language

Parsing the identify fields

2650
7
05-10-2010 08:52 AM
DonFreeman
Emerging Contributor
I seem to have lost my original post on this subject. My layer contains a field with a web address that I would like to appear as a hyperlink when the results are shown in an identify datagrid. Given the layer is selected via the code below (taken from the identify sample) how can I parse the datagrid and assign a hyperlink to the field containing an HTTP address?

 Private Sub cb_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
  Dim index As Integer = IdentifyComboBox.SelectedIndex
  If index > -1 Then
   IdentifyDetailsDataGrid.ItemsSource = _dataItems(index).Data
  End If
 End Sub

Thanks
0 Kudos
7 Replies
deleted-user-ATjHIWsdQYmT
Deactivated User
^^Bump^^

Trying to do the same thing here with no luck.  Anybody have a suggestion?
0 Kudos
DonFreeman
Emerging Contributor
^^Bump^^

Trying to do the same thing here with no luck.  Anybody have a suggestion?


Try this:
<UserControl.Resources>

  <Style x:Key="Hyper" TargetType="HyperlinkButton" 
   xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
   <Setter Property="Foreground" Value="Blue" />
   <Setter Property="Padding" Value="2,0,2,0"/>
   <Setter Property="Cursor" Value="Hand"/>
   <Setter Property="HorizontalContentAlignment" Value="Left"/>
   <Setter Property="VerticalContentAlignment" Value="Top"/>
   <Setter Property="Background" Value="Transparent" />
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="HyperlinkButton">
      <Grid Cursor="{TemplateBinding Cursor}" Background="{TemplateBinding Background}">
       <vsm:VisualStateManager.VisualStateGroups>
        <vsm:VisualStateGroup x:Name="CommonStates">
         <vsm:VisualState x:Name="Normal"/>
         <vsm:VisualState x:Name="MouseOver">
          <Storyboard>
           <ObjectAnimationUsingKeyFrames Storyboard.TargetName="UnderlineTextBlock" Storyboard.TargetProperty="Visibility" Duration="0">
            <DiscreteObjectKeyFrame KeyTime="0">
             <DiscreteObjectKeyFrame.Value>
              <Visibility>Visible</Visibility>
             </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
           </ObjectAnimationUsingKeyFrames>
          </Storyboard>
         </vsm:VisualState>
         <vsm:VisualState x:Name="Pressed">
          <Storyboard>
           <ObjectAnimationUsingKeyFrames Storyboard.TargetName="UnderlineTextBlock" Storyboard.TargetProperty="Visibility" Duration="0">
            <DiscreteObjectKeyFrame KeyTime="0">
             <DiscreteObjectKeyFrame.Value>
              <Visibility>Visible</Visibility>
             </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
           </ObjectAnimationUsingKeyFrames>
          </Storyboard>
         </vsm:VisualState>
         <vsm:VisualState x:Name="Disabled">
          <Storyboard>
           <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledOverlay" Storyboard.TargetProperty="Visibility" Duration="0">
            <DiscreteObjectKeyFrame KeyTime="0">
             <DiscreteObjectKeyFrame.Value>
              <Visibility>Visible</Visibility>
             </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
           </ObjectAnimationUsingKeyFrames>
          </Storyboard>
         </vsm:VisualState>
        </vsm:VisualStateGroup>
        <vsm:VisualStateGroup x:Name="FocusStates">
         <vsm:VisualState x:Name="Focused">
          <Storyboard>
           <DoubleAnimation Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
          </Storyboard>
         </vsm:VisualState>
         <vsm:VisualState x:Name="Unfocused"/>
        </vsm:VisualStateGroup>
       </vsm:VisualStateManager.VisualStateGroups>
       <TextBlock x:Name="UnderlineTextBlock"
          Text="{TemplateBinding Content}"
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
          Margin="{TemplateBinding Padding}"
          TextDecorations="Underline"
          Visibility="Collapsed"/>
       <TextBlock Canvas.ZIndex="1"
          x:Name="DisabledOverlay"
          Text="{TemplateBinding Content}"
          Foreground="Black"
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
          Margin="{TemplateBinding Padding}"
          Visibility="Collapsed"/>
       <ContentPresenter x:Name="contentPresenter"
          Content="{TemplateBinding Content}"
          ContentTemplate="{TemplateBinding ContentTemplate}"
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
          Margin="{TemplateBinding Padding}"/>
       <Rectangle x:Name="FocusVisualElement" 
        Stroke="#FF6DBDD1" 
        StrokeThickness="1" 
        Opacity="0" 
        IsHitTestVisible="false" />
      </Grid>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>

 </UserControl.Resources>


   <Grid x:Name="IdentifyGrid" 
         HorizontalAlignment="Center"
         VerticalAlignment="Top"
         Margin="0,0,0,10"
         >
     <Rectangle Style="{StaticResource PanelRectangle}" />
     <StackPanel x:Name="IdentifyResultsPanel" 
          Orientation="Vertical"
          HorizontalAlignment="Center"
          Margin="10,20,10,30">
      <TextBlock Width="180"
           TextWrapping="Wrap"
           Text="Click a feature on the map."
           FontWeight="Bold"
           HorizontalAlignment="Center" />
      <ComboBox x:Name="IdentifyComboBox"
          Width="180"
          SelectionChanged="cb_SelectionChanged"
          Margin="0,10,0,5" />

      <ScrollViewer x:Name="DataGridScrollViewer"
             HorizontalScrollBarVisibility="Visible"
             VerticalScrollBarVisibility="Visible"
             Width="180"
             Height="Auto"
             Margin="0,0,0,1">
       <slData:DataGrid x:Name="IdentifyDetailsDataGrid"
            AutoGenerateColumns="False"
            HeadersVisibility="None"
            Background="White"
            Margin="0,0,0,5"
            HorizontalScrollBarVisibility="Hidden"
            VerticalScrollBarVisibility="Hidden">
        <slData:DataGrid.Columns>
         <slData:DataGridTextColumn Width="90"
                  Binding="{Binding Path=Key}"
                  FontWeight="Bold" />
         <slData:DataGridTemplateColumn Width="300">
          <slData:DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
            <HyperlinkButton NavigateUri="{Binding Path=Value}"
               Content="{Binding Path=Value}"
               IsEnabled="{Binding Converter={StaticResource Convert_Bool}}"               
               TargetName="_blank" 
               Style="{StaticResource Hyper}" />
           </DataTemplate>
          </slData:DataGridTemplateColumn.CellTemplate>
         </slData:DataGridTemplateColumn>
        </slData:DataGrid.Columns>
       </slData:DataGrid>
      </ScrollViewer>
     </StackPanel>
    </Grid>
 
0 Kudos
deleted-user-ATjHIWsdQYmT
Deactivated User
Looking for resource:  StaticResource Convert_Bool
0 Kudos
DonFreeman
Emerging Contributor
Looking for resource:  StaticResource Convert_Bool


Add this to usercontrol.resources.
<Converters:Convert_Bool x:Key="Convert_Bool"/>
0 Kudos
deleted-user-ATjHIWsdQYmT
Deactivated User
Thanks Don.  Got it.  Now, how do I get fields to actually become links?  as a test, I added a field called "Link" and calc'd all the values to "http://www.google.com" however they do not show up as hyperlinks in the datagrid.  Am I missing something?
0 Kudos
DonFreeman
Emerging Contributor
Thanks Don.  Got it.  Now, how do I get fields to actually become links?  as a test, I added a field called "Link" and calc'd all the values to "http://www.google.com" however they do not show up as hyperlinks in the datagrid.  Am I missing something?


Gee it's been a while since I got this to work (with lots of help) and I don't remember exactly how it works. Does your URL show up in the datagrid as text? See if this helps in the code behind. It may be the DataItem class that you need.

 Public Class DataItem
  Private privateTitle As String
  Public Property Title() As String
   Get
    Return privateTitle
   End Get
   Set(ByVal value As String)
    privateTitle = value
   End Set
  End Property
  Private privateData As IDictionary(Of String, Object)

  Public Property Data() As IDictionary(Of String, Object)
   Get
    Return privateData
   End Get
   Set(ByVal value As IDictionary(Of String, Object))
    privateData = value
   End Set
  End Property
 End Class
0 Kudos
KevinMayo
Emerging Contributor
I was finally able to selectively create hyperlinks in the identify results.

You need to add an event handler to your results datagrid for LoadingRow in your xaml file.  The event handler is where I check for a field name and determine whether or not it's a known hyperlink field.  I use a helper function to get the current contents of the cell and swap them out with my hyperlink.

Not sure if it's the best approach, but it's working great in my app.

/// <summary>
/// Handles links in id results
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void IdentifyDetailsDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            KeyValuePair<string, object> pair = (KeyValuePair<string, object>)e.Row.DataContext;
            if (pair.Key == FLD_PCN)
            {
                DataGridColumn column = this.IdentifyDetailsDataGrid.Columns[1];
                FrameworkElement fe = column.GetCellContent(e.Row);
                FrameworkElement result = GetParent(fe, typeof(DataGridCell));
                if (result != null)
                {
                    DataGridCell cell = (DataGridCell)result;
                    TextBlock txt = new TextBlock() { Text = pair.Value.ToString(), TextDecorations = TextDecorations.Underline };
                    HyperlinkButton hyperlink = (HyperlinkButton)cell.Content;
                    hyperlink.Content = txt;
                    hyperlink.Foreground = new SolidColorBrush(Colors.Blue);
                    string strUri = LINK_PAPA + pair.Value.ToString();
                    Uri lnkUri = new Uri(strUri);
                    hyperlink.NavigateUri = lnkUri;
                    hyperlink.IsHitTestVisible = true;
                }
            }
.....else if blah blah blah
}

/// <summary>
        /// Helper function used to get individual cell contents
        /// </summary>
        /// <param name="child"></param>
        /// <param name="targetType"></param>
        /// <returns></returns>
        private FrameworkElement GetParent(FrameworkElement child, Type targetType)
        {
            object parent = child.Parent;
            if (parent != null)
            {
                if (parent.GetType() == targetType)
                {
                    return (FrameworkElement)parent;
                }
                else
                {
                    return GetParent((FrameworkElement)parent, targetType);
                }
            }
            return null;
        }
0 Kudos