I'm trying to leverage a hyperlink my xamDataGrid so that clicking on the text fires an event to my viewmodel with unique ID associated with my row.
<Style x:Key="HyperlinkStyle" TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<TextBlock Margin="{TemplateBinding Padding}" VerticalAlignment="Center" TextAlignment="Left"><Hyperlink Click="Navigate" Tag="{Binding}" >
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path =Value}"/>
</Hyperlink>
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I can figure out which row's hyperlink has been clicked by converting the hyperlink's tag to a datarecord, then converting the dataitem of that datarecord into my object and checking it's id property. I'm wondering if there isn't a more elegent way to go about this?
The DataContext of the CVP is the DataRecord. You can use the DataItem property to bind to. You can as well create a binding like DataItem.NavigationURL if you have an underlying property for that. You can also bind to the Index of the record. The binding expression would depend on your data structure and what exactly you are trying to achieve.
Using the sample code above I cannot get the hyperlinks to work, here's my snippet. The code compiles and I can run my app but the byperlinks do not appear. Maybe I'm missing something else???
<igDP:XamDataGrid Height="Auto" Name="xamDataGridWebLinks" Width="Auto" DataSource="{Binding}" Background="Transparent" Foreground="White" > <igDP:XamDataGrid.Resources> <Style x:Key="HyperlinkStyle" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <TextBlock Margin="{TemplateBinding Padding}" VerticalAlignment="Center" TextAlignment="Left"> <Hyperlink Click="Navigate" Tag="{Binding}" > <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path =Value}"/> </Hyperlink> </TextBlock> </ControlTemplate> </Setter.Value> </Setter> </Style> </igDP:XamDataGrid.Resources> </igDP:XamDataGrid>
Thanks, Troy
Hello Troy,
I tried this control template. It works. I notice that you have set the x:key property of the style, but have you applied that to the field's cellvaluepresenterstyle property? Please note that if you have a style, you have to apply it manually.
No I have not applied the key property to the field's cellvaluepresenterstyle property, I just tried to do that by typing the key value 'HyperlinkStyle' in the cellvaluepresenterstyle property field when I tab off it gives me an error that reads, "Property value is not valid". I obviously doing something wrong here.
I know nothing about this so if your assuming I have some grasp on what I'm doing then you're giving me too much credit. :-)
Troy
Troy,
In the ControlTemplate, you can create bindings to different properties, depending on the RelativeSource that you set. Here are couple of options:
1. No RelativeSource set. This will default to the DataRecord. The DataRecord exposes the Cells collection, so you can bind to any cell, for example - Path - Cells[2].Value or Path - Cells[ID].Value. You can also bind to the underlying object's property, like so : Path - DataItem.ID.
2. TemplatedParent - this will give you the CellValuePresenter itself. You can bind to its properties.
3. FindAncestor (with AncestorType). This will search for an element of the specified type in the ancestors elements chain and bind to it. For Example - RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type igDP:DataRecordPresenter}}
4. Self. This will bind to the current element.
As a side note, if you want to get wild and create a binding between two or more properties with different types (for example int and Brush), you can use IValueConverter or IMultiValueConverter in the binding expression.
OK, got it working. Thanks for the beginners version.
If I could trouble you for an explanation or a beginners version of the post eariler on in this thred where you wrote,
"The DataContext of the CVP is the DataRecord. You can use the DataItem property to bind to. You can as well create a binding like DataItem.NavigationURL if you have an underlying property for that. You can also bind to the Index of the record. The binding expression would depend on your data structure and what exactly you are trying to achieve."
That's what I'm trying to do next...
Then, here it is how your xam data grid should look like:
<igDP:XamDataGrid Name="xamDataGrid1" >
<igDP:XamDataGrid.Resources>
<!--Hyperlink Style defined here-->
</igDP:XamDataGrid.Resources>
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:Field>
<igDP:Field.Settings>
<igDP:FieldSettings CellValuePresenterStyle="{StaticResource MyStyleKeyHere}"/>
</igDP:Field.Settings>
</igDP:Field>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
Hope this helps.