I used a CellValuePresenter to display hyperlinks in the grid successfully after following examples in this forum.
<infra:XamDataGrid.Resources>
<Style TargetType="{x:Type infra:CellValuePresenter}" x:Key="hyperlinkCell"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type infra:CellValuePresenter}"> <TextBlock Margin="{TemplateBinding Padding}"> <Hyperlink NavigateUri="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}" RequestNavigate="Hyperlink_RequestNavigate"> <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}"/> </Hyperlink> </TextBlock> </ControlTemplate> </Setter.Value> </Setter> </Style> </infra:XamDataGrid.Resources>
<infra:FieldLayout.Fields> <infra:Field Name="CoverageName" Width="280" Label="{Localize coverageName}"> <infra:Field.Settings> <infra:FieldSettings AllowEdit="False" LabelClickAction="Nothing" KeyboardNavigation.IsTabStop="False" CellValuePresenterStyle="{StaticResource hyperlinkCell}"> </infra:FieldSettings> </infra:Field.Settings> </infra:Field>
With this all my coverages are hyperlinked now in the xamdatagrid.
I need to optionally (based on field value in this case CoverageName) show the hyperlink so some values will be hyperlinked and some will not in the xamdatagrid that is using the cellvaluepresenter.The coveragenames are dynamically filled in the xamdatagrid. Is this possible? Any examples would be helpful.
Hello Ritesh,
Thank you for your post.
To have some of your XamDataGrid cells be hyperlinked and others not based on a value in another field, I would recommend continuing with the idea of the CellValuePresenter style that you are currently writing. In that style, you can achieve your requirement by adding a DataTrigger that binds to the data context of the CellValuePresenter, which is the DataRecord that particular CellValuePresenter sits in. This DataRecord has a data item property that returns the item in the bound collection to the XamDataGrid.DataSource property, which the record represents. Knowing this, you can bind the DataTrigger to {Binding DataItem.CoverageName} and check for a value correspondent with a CoverageName value that designates that a certain cell should be linked. If this condition holds true, then you can use a Setter to set the template that you are setting above to add a hyperlink to that particular cell.
If the CoverageName will have multiple different values, I would recommend using a Converter in the DataTrigger binding to examine the value and return a value that is consistent with the value that the DataTrigger is looking for.
I have attached a sample project to demonstrate the above. I hope this helps you.
Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate DeveloperInfragistics Inc.www.infragistics.com/support
Hi Andrew,
Now I want to fire an event when i click on the hyperlink.When I am using the the event requestnavigate in the following code in bold:
<DataTrigger Binding="{Binding DataItem.IsHyperlink}" Value="True"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <TextBlock> <Hyperlink NavigateUri="{Binding RelativeSource={RelativeSource AncestorType={x:Type infra:CellValuePresenter}}, Path=DataContext.DataItem.CoverageName}" RequestNavigate=""> <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type infra:CellValuePresenter}}, Path=Value}"/> </Hyperlink> </TextBlock>
</DataTemplate> </Setter.Value> </Setter> </DataTrigger>
I am getting the following error:
The event 'RequestNavigate' cannot be specified on a Target tag in a Style. Use an EventSetter instead.
Also u have binded the IsLink in the datatrigger which is a field name is xamdatagrid.Can I create a property in my code and set it to false for coverage for I dont require an hyperlink and set it in datatrigger.So,that when this property is false cellvaluepresenterstyle (i.e. hyperlink) will not set for that coverage name.
I am not entirely sure why the attempt to set the RequestNavigate event is throwing this error, but to get around this, I would recommend writing a keyed Style for Hyperlink with an EventSetter for the RequestNavigate event. Then, you should be able to apply this style to your Hyperlink in the ContentTemplate via a StaticResource binding. This should allow you to handle the RequestNavigate event.
Regarding the binding in the data trigger, it is true that the IsLink is a field name in the XamDataGrid, but it doesn't have to be. What is important here is that the IsLink property exists on the underlying data items bound to the XamDataGrid. If you create a property on your underlying data item, you can always bind to it in the CellValuePresenter, as the CellValuePresenter's data context has access to that data item through the DataItem property of the DataRecord that makes up that data context.
I have attached a modified version of the original sample project I had sent you. This sample project removes the IsLink field from the XamDataGrid, but still binds to the property that it was represented by. It also demonstrates the hyperlink RequestNavigate resolution as well.
I hope this helps. Please let me know if you have any other questions or concerns on this matter.
Thanks for all the solutions.Hope it may help some others also.
Have a nice day!!!!!
You are welcome. I am glad these solutions were of some help to you and I too hope it will help others at some point.