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
Hello Ammad,
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?
Let me know if you have any questions.
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
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, 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.
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.
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.
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.