Select to view content in your preferred language

Hyperlinking possibilities

2043
9
07-22-2010 06:04 AM
TamasHajdu
Emerging Contributor
Hi,

We are developing a project about a certain infrastructure and we are going to include hyperlinking capabilities as well in this project. We have already implemented an identify function which takes the results into a datagrid.
The fact is that certain layers have LINK fields and we want to access these fields and open the url in a new browser window by clicking on the right row in the datagrid results.
Is this possible somehow?

Another idea was to include a hyperlinking button like it is in the out-of-the-box web applications.


We are using VS2010, Silverlight 4 API, and ArcGIS API for Silverlight 2.0 .
We would appreciate any help!
Thank You guys in advance,

Sampath Sri Ranawaka & Tamas Hajdu
0 Kudos
9 Replies
AliMirzabeigi
Emerging Contributor
Sampath and Tamas,

You would need to set AutoGenerateColumns property of the DataGrid to False and use templated columns for the fields representing URLs. For example, consdier records with the following fields: FirstName, LastName, and WebSite in which the last one contains hyperlinks. The code snippet below shows how to use a DataGrid TemplateColumn for WebSite attribute in your records:

<sdk:DataGrid x:Name="MyDataGrid" AutoGenerateColumns="False" Height="100">
[INDENT]<sdk:DataGrid.Columns>
[INDENT]<sdk:DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" />
<sdk:DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" />
<sdk:DataGridTemplateColumn Header="Web Site">
[INDENT]<sdk:DataGridTemplateColumn.CellTemplate>
[INDENT]<DataTemplate>
[INDENT]<HyperlinkButton Content="{Binding WebSite}" TargetName="_blank" NavigateUri="{Binding WebSite}" />
[/INDENT]</DataTemplate>
[/INDENT]</sdk:DataGridTemplateColumn.CellTemplate>
[/INDENT]</sdk:DataGridTemplateColumn>
[/INDENT]</sdk:DataGrid.Columns>
[/INDENT]</sdk:DataGrid>

Hope this helps.
0 Kudos
TamasHajdu
Emerging Contributor
Hi Ali,

Thank you for answering.
We have autogeneratecolumns property false, since we narrowed the number of fields to display and therefore we did not need all the fields.
Your code is quite understandable, but the only thing we are still interested in is that we need some additional csharp code for implementing or not? I think we do, but hearing your opinion would bw nice.
We were as well wandering about the "Website" you included in your xaml code. We guessed it was a variable. Is that right?
Have a nice day!
cheers,
Sampath and Tamas
0 Kudos
AliMirzabeigi
Emerging Contributor
Tamas,

Off course you would also need to develop some C# code. The data structure I used in my simple code snippet was as the following:
public class person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string WebSite { get; set; }
}

And in the page constructor I had:

persons = new List<person>(2);
persons.Add(new person()
{
FirstName = "John",
LastName = "Doe",
WebSite = "http://www.esri.com"
});
persons.Add(new person()
{
FirstName = "Jane",
LastName = "Doe",
WebSite = "http://resources.esri.com"
});

dg.ItemsSource = persons;
0 Kudos
BenjaminClift
Emerging Contributor
I also have a question on hyperlinking.  I am trying to use a button to go to a website. 

The web address is the same except for a number.  That number is in the attributes of one of the layers in the map service.  I am already pulling the attributes into a datagrid using the identify code.  I have a button below the grid that I want to then press and go to a link that is unique to the identified feature.

example:
attribute table:
123
456
789

website for each feature:
www.esri_123.com
www.esri_456.com
www.esri_789.com

The website link is the same except for inserting a different value depending on the attribute field.


Thanks for the help.
0 Kudos
AliMirzabeigi
Emerging Contributor
On Click event of your button you can get the selected item in your DataGrid:

person row = MyDataGrid.SelectedItem as person;

Then you can build the Uri and navigate to the corresponding website:

string webAddress = string.Format("http://www.esri_{0}.com", row.WebSite);
System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(webAddress),"_blank");
0 Kudos
TamasHajdu
Emerging Contributor
I have tried with a xaml code like this:

<slData:DataGrid x:Name="IdentifyDetailsDataGrid" Grid.Row="2" AutoGenerateColumns="False" HeadersVisibility="All"
                            Background="White" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
                            Margin="0,2,0,0">
                       
                        <slData:DataGrid.Columns>
                            <slData:DataGridTextColumn Binding="{Binding Key}" FontWeight="Bold" Header="Field"/>
                           
                            <slData:DataGridTemplateColumn Header="Value">
                           
                            <slData:DataGridTemplateColumn.CellTemplate>

                                <DataTemplate>

                                        <HyperlinkButton Content="{Binding Value}" TargetName="_blank" NavigateUri="{Binding WebSite}" />

                                </DataTemplate>

                            </slData:DataGridTemplateColumn.CellTemplate>
                            </slData:DataGridTemplateColumn>
                           
                        </slData:DataGrid.Columns>
                       
                       
                    </slData:DataGrid>



And the C# code is the following:

private void IdentifyDetailsDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string row = IdentifyDetailsDataGrid.SelectedItem.ToString();
            System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(row), "_blank");
        }

What I dont know is how to make it only available for one cell (which contains url) to be hyperlinked.
If I use the templatecolumn, all the values in the column will be highlighted, and want only that field to be highlighted which contains the url. Is that possible somehow to narrow the criteria somehow like this?

I use the following code to populate the datagrid:

        void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = IdentifyComboBox.SelectedIndex;
           
            if (index > -1)
                IdentifyDetailsDataGrid.ItemsSource = _dataItems[index].Data;
        }

and the dataitems class is this:

public class DataItem
        {
            public string Title { get; set; }
            public IDictionary<string, object> Data { get; set; }
        }

Any help would be appreciated!
Thanks!

Tamas
0 Kudos
AliMirzabeigi
Emerging Contributor
Tamas,

On the SelectionChanged event of your DataGrid check for CurrentColumn object, i.e. (sender as DataGrid).CurrentColumn; You can check for its Header for example.
0 Kudos
KevinSesock
Emerging Contributor
Anyone get this to work? I'm trying to do what I can only assume is the same thing, make a value in a KeyValuePair be a URL. Since these are showing up as a Row, not a column, we can't use the Column Header to help us. To do this though, my assumption has been to use (sender as DataGrid).SelectedItem, not CurrentColumn.Header. Any thoughts of how to pull the Key and Value out of SelectedItem, or in the alternate, a workaround for this issue?
0 Kudos
TamasHajdu
Emerging Contributor
Altough the project issue was a long ago, I dug out a piece of code which I hope would help you to solve your problem. It worked in our project with the URLs we passed.

Code behind part:

private void IdentifyDetailsDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                string row = IdentifyDetailsDataGrid.SelectedItem.ToString();
                int at = row.IndexOf(",");
                string web_proc = row.Remove(0, (row.Length - (row.Length - at - 1)));
                string website = web_proc.Remove(web_proc.Length - 1, 1);
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(website), "_blank");
            }
            catch
            {
            }
        }

XAML part:

<slData:DataGrid x:Name="IdentifyDetailsDataGrid" Grid.Row="4"
                            AutoGenerateColumns="False"
                            HeadersVisibility="All"
                            Background="White"
                            HorizontalScrollBarVisibility="Auto"
                            VerticalScrollBarVisibility="Auto"
                            SelectionChanged="IdentifyDetailsDataGrid_SelectionChanged"
                            Margin="0,1,0,0">
                        <slData:DataGrid.Columns>
                            <slData:DataGridTextColumn Binding="{Binding Key}" Header="Field" FontWeight="Bold"/>
                            <slData:DataGridTextColumn Binding="{Binding Value}" Header="Value" />
                        </slData:DataGrid.Columns>
                    </slData:DataGrid>

If I take a look at it right now, what it does, is, that if you click on an element of a datagrid, it examines whether the clicked value is a URL or not. If it isnt, nothing happens (this is what the catch is there for) but if there is a URL it is opened in a new browser window.
I am not a guru in this stuff, and regarding the method it may seem pre-historic but it was working for us. 🙂
I hope my post was helpful.

cheers

Tamas
0 Kudos