Hi Team,i have a small issue that i need to create the XamGrid column from code behind and catch the Hyperlink Click event._grid.Columns.Add(new HyperlinkColumn { Key = columnKey, HeaderText = FormatColumnHeader(entry.Key), //FormatString = formatString, HorizontalContentAlignment = rightAlign ? HorizontalAlignment.Right : HorizontalAlignment.Left, Width = new ColumnWidth( firstColumn ? ResultGridView.MeasureStringWidth("00000000") : columnWidth, false), MinimumWidth = 30, CellStyle = Application.Current.Resources["CellStyle"] as Style, HeaderTemplate = Application.Current.Resources["HeaderTemplate"] as DataTemplate, IsMovable = !firstColumn, IsHideable = !firstColumn, IsResizable = true, IsSortable = true, });this is my codeand the cell Style is <Style x:Key="CellStyle" TargetType="ig:CellControl"> <Setter Property="Foreground" Value="{StaticResource ForegroundBrush}" /> <Setter Property="FontSize" Value="{StaticResource SmallFontSize}" /> <Setter Property="FontFamily" Value="{StaticResource ContentFontFamily}" /> <Setter Property="MinHeight" Value="30" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="HorizontalContentAlignment" Value="Left" /> <Setter Property="Background" Value="{StaticResource CellItemNormalBackgroundBrush}" /> <Setter Property="BorderBrush" Value="{StaticResource CellItemNormalBorderBrush}" /> <Setter Property="BorderThickness" Value="0,0,1.5,1" /> <Setter Property="Padding" Value="6,4" /> <Setter Property="ResizingThreshold" Value="20" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ig:CellControl"> <Grid> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal" /> <VisualState x:Name="MouseOver"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="hoverElem" Storyboard.TargetProperty="Visibility"> <DiscreteObjectKeyFrame KeyTime="00:00:00"> <DiscreteObjectKeyFrame.Value> <Visibility>Visible</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Alternate"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="altElem" Storyboard.TargetProperty="Visibility"> <DiscreteObjectKeyFrame KeyTime="00:00:00"> <DiscreteObjectKeyFrame.Value> <Visibility>Visible</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup x:Name="SelectedStates"> <VisualState x:Name="NotSelected" /> <VisualState x:Name="Selected"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="rowSelected" Storyboard.TargetProperty="Visibility"> <DiscreteObjectKeyFrame KeyTime="00:00:00"> <DiscreteObjectKeyFrame.Value> <Visibility>Visible</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ActiveSelected" Storyboard.TargetProperty="Visibility"> <DiscreteObjectKeyFrame KeyTime="00:00:00"> <DiscreteObjectKeyFrame.Value> <Visibility>Visible</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup x:Name="ActiveStates"> <VisualState x:Name="InActive" /> <VisualState x:Name="Active" /> </VisualStateGroup> <VisualStateGroup x:Name="EditingStates"> <VisualState x:Name="NotEditing" /> <VisualState x:Name="Editing" /> </VisualStateGroup> <VisualStateGroup x:Name="FixedStates"> <VisualState x:Name="UnFixed" /> <VisualState x:Name="Fixed" /> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Border x:Name="Root" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" /> <Border x:Name="altElem" Background="{StaticResource CellItemAltNormalBackgroundBrush}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Visibility="Collapsed" /> <Border x:Name="hoverElem" Margin="{TemplateBinding BorderThickness}" Background="{StaticResource CellItemHoverBackgroundBrush}" BorderThickness="0" Visibility="Collapsed" /> <Border x:Name="rowSelected" Margin="{TemplateBinding BorderThickness}" Background="{StaticResource CellItemSelectedBorderBrush}" BorderThickness="0" Opacity="0.25" Visibility="Collapsed" /> <Border x:Name="ActiveSelected" BorderBrush="{StaticResource CellItemSelectedBorderBrush}" BorderThickness="2" Visibility="Collapsed" /> <ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>Please Help me out
1 More thing. When hyperlink is clicked it opens the URL in the same window. rather than in a separate window.
Hello Ammad,
Thank you for contacting Infragistics.
1. What specific requirements do you have when clicking the hyperlink other than navigation?
The HyperlinkColumn doesn't expose a click event directly. You may utilize the XamGrid's CellClicked event, which fires when the user left clicks on a cell.
2. You set the HyperLinkColumn's TargetName property to "_new" or "_blank" to navigate the address in a new page or tab. "_self" should be the same page.
<ig:HyperlinkColumn TargetName="_blank">
Let me know if you have any questions.
Hi Micheal, Thank you for your reply. There are multiple columns in the Grid, and we dont have any specific column for Hyperlink. It varies depending on the Name of the Column. If i use CellClicked event, It will fire every single time we click on the cell, even if its not on the hyperlink itself. is there a way that i can know in the CellClicked event that the click was done on the hyperlink specifically and not on the empty space around the text? Regards,Ammad
Thank you for following up. The CellClicked event should only be raised when a Cell element is clicked.
The Cell object is exposed as an event argument for the CellClicked event. Whereby you can track down the Cell.Column associated.
In the event you can place an "if check" to determine if the sender of the event is indeed a type Cell, but and check it's Column property.
Hi Micheal,I understand that CellClicked will only be raised when cell element is clicked. But it is being raised multiple times when i even click the empty area in the cell. and there are a couple of issues as well.1. as you said I should Check the type Cell and i did. but the sender is always XamGrid.
2. the hyperlink click event is fired anyway and even before we reach to CellClicked.
3. Please understand I need to hook the Hyperlink Click event. so I can manipulate the values as required. I would be grateful if you would please provide an example. below is the code of how i am adding a column. _grid.Columns.Add(new HyperlinkColumn { Key = columnKey, HeaderText = FormatColumnHeader(entry.Key), //FormatString = formatString, HorizontalContentAlignment = rightAlign ? HorizontalAlignment.Right : HorizontalAlignment.Left, Width = new ColumnWidth( firstColumn ? ResultGridView.MeasureStringWidth("00000000") : columnWidth, false), MinimumWidth = 30, CellStyle = Application.Current.Resources["CellStyle"] as Style, HeaderTemplate = Application.Current.Resources["HeaderTemplate"] as DataTemplate, IsMovable = !firstColumn, IsHideable = !firstColumn, IsResizable = true, IsSortable = true, TargetName = "_new", }); _grid.CellClicked += _grid_CellClicked;
void _grid_CellClicked(object sender, CellClickedEventArgs e) { if (sender is Cell) {
} }Regards
Ammad Javaid
Hello,
In the CellClicked event you are going to want to inspect the "e" and not "sender".
"e" contains the Cell where you can access the value and the column it resides in.
Which platform are you working with? (eg. Silverlight, WPF)
Let me know if you have any additional questions.
Hi, I am working on Silverlight as the tag suggests thank you for the information. I got round about that. from the event 'e'. but the information doesn't resolve my issue.
Ammad Javaid said:2. the hyperlink click event is fired anyway and even before we reach to CellClicked.3. Please understand I need to hook the Hyperlink Click event. so I can manipulate the values as required.
3. Please understand I need to hook the Hyperlink Click event. so I can manipulate the values as required.
Please understand the Hyperlinks are fired regardless and even before the breakpoint hits the CellClicked Event. I need some specifics from your side which will help me out. A sample code would be great.Thanks.
Ammad
Hello Ammand,
I reproduced the behavior you described where the page opens before the CellClicked event fires but I couldn't find any possible work around so I decided to use a TemplateColumn instead where you can embed a HyperLinkButton and handle it's click event.
The following will allow you to trap the link before it's opens the page and handle your validation accordingly.
SilverlightApplication1.zip
Hi Michael, thank you for your help. But this doesn't help my issue. I need the hyperlinkcolumn be created from code behind as we need the columns be autogenerated.Is It possible for you to send me a TemplateColumn setup from Codebehind for this?Regards
Thank you for following up. The code below demonstrates how to add a template column programmatically. It can be added to the sample by handling the MainPage's Loaded event.
eg.
private void MainPage_Loaded(object sender, RoutedEventArgs e) { xamGrid1.AutoGenerateColumns = false; var templateColumn = new TemplateColumn { Key = "CategoryName", HeaderText = "HyperLink", ItemTemplate = new DataTemplate { DataType = typeof(Uri), } }; xamGrid1.Columns.Add(templateColumn); }
Since you will be required to have a DataTemplate for the HyperLinkButton you still need to write it in XAML and access as a resource. In order to get the code above to work correctly I moved the DataTemplate to the UserControl's Resources on the main page. Although you cannot programmatically create it, alternatively you can load it from a XAML string in code like this:
public static DataTemplate Create(Type type) { return (DataTemplate) XamlReader.Load( @"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007""> <" + type.Name + @"/> </DataTemplate>" ); }
Note, you cannot auto generate columns and add them programmatically as well. Can you clarify this requirement?