Convert a string to a link in a query result

1241
10
12-07-2010 03:28 AM
PaulHuppé
Occasional Contributor
Hi,

I am doing a query task and getting results back.  In the feature set I get back, in the attributes, one of the attributes is an ID which I want to use in a URL.  In the results grid, I would like to see just a button or a hyperlinked word which would open a new window to the URL I would build.

Currently, when the query task completes, the results grid is populated using:

QueryDetailsDataGrid.ItemsSource = args.FeatureSet.Features;


Obviously, this will have to change if I want to insert some type of link.  I have tried the following code:

                List<Graphic> lstGraphics = new List<Graphic>();
                foreach (Graphic g in args.FeatureSet.Features)
                {
                    if (g.Attributes.ContainsKey("GEOSCANREC"))
                    {
                        g.Attributes["GEOSCANREC"] = "<a href=\"http://geopub.nrcan.gc.ca/html/view_e.php?id=" + g.Attributes["GEOSCANREC"] + "\">GO</a>";
                    }
                    lstGraphics.Add(g);
                }
                QueryDetailsDataGrid.ItemsSource = lstGraphics;


which inserts the URL in the grid, but it is not an active link.  Is there a way to change that so the user can click to open the link in a new window?

Thanks,
Paul
0 Kudos
10 Replies
PaulLeedham
New Contributor III
Have you tried?:
GEOSCANREC"] = "<a href=\"http://geopub.nrcan.gc.ca/html/view_e.php?id=" + g.Attributes["GEOSCANREC"] + "\" Target=_Blank>GO</a>";

We handle our links by modifying the class, but I think this should also work. You may want to also add some error handling to notify the user if they have their pop-up blocker turned on.

Thanks,
0 Kudos
PaulHuppé
Occasional Contributor
Hi,
Yes, I know about the Target="_Blank" property.  You can see what I currently get in the attached image.  I do not want to see the URL, but maybe just the word GO or a button maybe.
Paul
0 Kudos
PaulLeedham
New Contributor III
Are you adding this field as a hyperlink button to your list or grid?
After you have defined your class or itemsource for the data container you would bind your control like so:
<HyperlinkButton Content="GO" NavigateUri="{Binding [GEOSCANREC]}" TargetName="_blank" />

Thanks,
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I don't know your context but it looks like you are trying to display HTML inside Silverlight. That's not that easy.

If you stay inside Silverlight, you should use an 'HyperlinkButton' and set the NavigateUri property.

Inside a datagrid, you can define a column like this:

 
<slData:DataGridTemplateColumn>
  <slData:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <HyperlinkButton NavigateUri="{Binding Attributes[GEOSCANREC], StringFormat='http://geopub.nrcan.gc.ca/html/view_e.php?id=\{0\}'}" Content="Click here" TargetName="_blank" />
    </DataTemplate>
  </slData:DataGridTemplateColumn.CellTemplate>
</slData:DataGridTemplateColumn>
0 Kudos
PaulHuppé
Occasional Contributor
I need to add all fields dynamically.  So I currently only have the Datagrid control in my XAML page with no template for the columns.
0 Kudos
PaulHuppé
Occasional Contributor
Hi Dominique,
That is exactly what I want to do, but I need to do all of this in code behind.  How do you define this template in code behind?

I don't know your context but it looks like you are trying to display HTML inside Silverlight. That's not that easy.

If you stay inside Silverlight, you should use an 'HyperlinkButton' and set the NavigateUri property.

Inside a datagrid, you can define a column like this:

 
<slData:DataGridTemplateColumn>
  <slData:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <HyperlinkButton NavigateUri="{Binding Attributes[GEOSCANREC], StringFormat='http://geopub.nrcan.gc.ca/html/view_e.php?id=\{0\}'}" Content="Click here" TargetName="_blank" />
    </DataTemplate>
  </slData:DataGridTemplateColumn.CellTemplate>
</slData:DataGridTemplateColumn>
0 Kudos
DominiqueBroux
Esri Frequent Contributor
You can do the same thing by code, even if the creation of DataTemplate is not straightforward with Silverlight (need to parse a string with XamlReader).

Adding a column by code looks like:
 string attributeName = "GEOSCANREC";
DataTemplate cellTemplate = (DataTemplate)System.Windows.Markup.XamlReader.Load( @"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007""> 
    <HyperlinkButton NavigateUri=""{Binding Attributes[" + attributeName + @"], StringFormat='http://geopub.nrcan.gc.ca/html/view_e.php?id=\{0\}'}"" Content=""Click here"" TargetName=""_blank"" />
  </DataTemplate>");
queryDetailsDataGrid.Columns.Add(new DataGridTemplateColumn() { Width = new DataGridLength(100), Header = "Hyperlink", CellTemplate = cellTemplate });
0 Kudos
PaulHuppé
Occasional Contributor
That works. Great!  Like you said, this is not straightforward.
Thanks,
Paul
0 Kudos
AndrewHargreaves
Occasional Contributor II
Thanks for the above hint, I used it to hyperlink email address I obtained from a query as shown below:

<slData:DataGridTemplateColumn  Header="Email">
     <slData:DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
               <HyperlinkButton NavigateUri="{Binding Attributes[Email], StringFormat='mailto:\{0\}'}" Content="Email" TargetName="_blank" Foreground="Blue" FontWeight="Normal"/>
          </DataTemplate>
     </slData:DataGridTemplateColumn.CellTemplate>
</slData:DataGridTemplateColumn>
0 Kudos