I have a XamDataGrid in which there is a Column COUNT to which the values are assigned dynamically.
What i want is if the column cell value is 2 then convert it to hyperlink with text CLICK and if the cell value is 1 then it should remain as it is(no hyperlink, no text change).
Kind Regards
Hi Sachin,
Thank you for posting to Infragistics Community!
I have been looking into your question and created a small sample with a XamDataGrid to demonstrate how your requirement can be achieved. You can find it attached below.
The approach involves declaring a keyed style, targeting the CellValuePresenter type and assigning it as the CellValuePresenterStyle property of your target column. The style would introduce DataTrigger for the corresponding values and bound to the target DataItem’s propery. Then, for any value, the Template property could be set to a custom ControlTemplate, for example such that it contains a hyperlink.
Here is a snippet of the sample grid’s markup:
<igWPF:XamDataGrid DataSource="{Binding Data}" Height="400"> <igWPF:XamDataGrid.Resources> <Style x:Key="HyperlinkStyle" TargetType="Hyperlink"> <EventSetter Event="RequestNavigate" Handler="Hyperlink_RequestNavigate"></EventSetter> </Style> <Style TargetType="{x:Type igWPF:CellValuePresenter}" x:Key="CoalCVPStyle"> <Style.Triggers> <DataTrigger Binding="{Binding Path=DataItem.Coal}" Value="1"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igWPF:CellValuePresenter}"> <TextBlock> <Hyperlink NavigateUri="{Binding Path=DataItem.Hyperlink}" Style="{StaticResource HyperlinkStyle}"> <Run Text="{Binding Path=DataItem.Coal}" /> </Hyperlink> </TextBlock> </ControlTemplate> </Setter.Value> </Setter> </DataTrigger> <DataTrigger Binding="{Binding Path=DataItem.Coal}" Value="2"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igWPF:CellValuePresenter}"> <TextBlock Text="{Binding Path=DataItem.Coal}"></TextBlock> </ControlTemplate> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style> </igWPF:XamDataGrid.Resources> <igWPF:XamDataGrid.FieldLayoutSettings> <igWPF:FieldLayoutSettings AutoGenerateFields="False" /> </igWPF:XamDataGrid.FieldLayoutSettings> <igWPF:XamDataGrid.FieldLayouts> <igWPF:FieldLayout> <igWPF:FieldLayout.Fields> <igWPF:Field Name="Country" /> <igWPF:TextField Name="Region" /> <igWPF:TextField Name="Coal" CellValuePresenterStyle="{StaticResource CoalCVPStyle}"/> </igWPF:FieldLayout.Fields> </igWPF:FieldLayout> </igWPF:XamDataGrid.FieldLayouts> </igWPF:XamDataGrid>
I hope this helps achieve your requirement. Please, test this on your side and let me know of any other questions on the matter.
Best regards,Bozhidara PachilovaAssociate Software Developer
2843.XDGConditionalHyperlink.zip
Hello Bozhidara Pachilova,
Thank you for helping me out with your sample.
I have another query related to my project, how can i make my cell value to hyperlink and change its text to SPECIAL if my cell value have any special symbol(~) in it.
Ex-:
COUNT ="Test~Case~1"
if count contains symbol(~) ,Cell should look like SPECIAL with hyperlink on it.
I am glad that you find my suggestion helpful!
To address your new query, with this requirement included, I would suggest adopting a slightly different approach given the fact that the choice would be between at least three different templates and that a simple equality check for the field’s value would not suffice to determine whether the string contains the ‘~’ character in the Style’s triggers.
So, I believe you will find the Apply Cell Value Templates Conditionally topic in our documentation very helpful on the matter. This approach involves declaring your field as a TemplateField and setting a “selector” that would provide conditional logic for applying different templates based on the field value. The selector class should extend from the DataTemplateSelector class and implement the SelectTemplate method which returns a DataTemplate type.
I have modified the previous sample to demonstrate this suggestion. In the XamDataGrid’s Resources collection is included the custom template selector “MyDisplayTemplateSelector” as well as three different data templates, for the three separate cases – when the value is “2”, “1” and when there is the “Special” case. As you will see, the templates leverage the custom TemplateEditorValueBinding markup extension for binding to the ValueEditor.Value property of a TemplateEditor. A slightly more complicated binding is also introduced to bind to the template editor’s DataContext in order to access the “Hyperlink” property on the DataItem. Finally, the custom selector is assigned as the TemplateField’s DisplayTemplateSelector. As you will read in the referenced topic, the DisplayTemplate provides a template for cells that are not in edit mode for a specific TemplateField. Should an EditTemplate be provided in a similar way, the EditTemplateSelector has to be provided.
In conclusion, please check out these resources and let me know if I may be of any further assistance on the matter.
0333.XDGConditionalHyperlink2.zip
Hello Bozhidara Pachilova
That worked. Thanks!
Hello Sachin,
Yes, this is also expected, as even if the DisplayText is set to “Special”, separate values such as “"Test~Case~1" and "Test~Case~2" are still treated as such. Therefore, if you have multiple entries of the former and also multiple of the latter in your data, the total appearances of “Special” in the filter dropdown would be 2. Therefore, the suggested solution assumed that there were only unique values satisfying the condition to contain “~”, as no information regarding the opposite was previously provided.
Fortunately, there are still options to workaround this. My suggestion is to use a Converter on the TemplateField, whose Convert method returns the “Special” string in case the original values contains the ‘~’ character.
internal class MyVlaueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value.ToString().Contains('~')) { return "Special"; } else { return value; } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
Please, be sure to also modify the DataTemplateSelector, so that it sets the “SpecialTemplate” incase the value is equal to “Special”, with the new logic introduced:
//.. else if (val.Equals("Special")) { dataTemplate = editor.FindResource("SpecialTemplate") as DataTemplate; }
Additionally, handling the RecordFilterDropDownOpening event is no longer needed.
Attached you will find the latest version of the sample demonstrating this suggestion. Please, test it on your side and let me know how it behaves.
Best regards,Bozhidara Pachilova
8156.XDGConditionalHyperlink3.zip
Hi Bozhidara Pachilova,
I have tried the code you have provided but it again gave an issue for my project, In my project there are more than 50 items in the dropdown list out of which approx. 30 are having special character (~) for which i wanted them to be shown as "Special" on dropdown menu.
My requirement was that if there are 30 items with special character(~) inside them they should be shown with text "Special" only SINGLE time on the dropdown list not 30 times and if I select "Special" on the dropdown list it should show all the 30 items with text "Special" on my grid.
Kind Regards,
You could handle this by leveraging the grid’s RecordFilterDropDownOpening event, from which you can set the target item’s DisplayText to another value, for instance:
private void XamDataGrid_RecordFilterDropDownOpening(object sender, Infragistics.Windows.DataPresenter.Events.RecordFilterDropDownOpeningEventArgs e) { foreach (var item in e.DropDownItems) { if (item.DisplayText.ToString().Contains("~")) { item.DisplayText = "Special"; } } }
Best regards, Bozhidara Pachilova
Thanks for your help and for your sample.
The sample you gave me worked for my condition but it raised another issue on my XamDataGrid FilterRecords Property. As i have explained that my requirement was that if i had a string with special character(~), i wanted my string to be converted to a Hyperlink with the text "Special" on it.
Now my issue is I have "AllowRecordFiltering" property applied to my Template field which is set to "True", which when i am trying to filter my record the string with (~) shows up there rather than my text Special on Filter Record dropdown.
In my FilterRecord DropDown i want Special text to appear rather than Test~Case~1.Please check attached screenshot.
I am glad that this helps!
For the sake of completeness, I am reattaching the sample again due to the previously missing selector class file.
5852.XDGConditionalHyperlink2.zip