ryrich
ryrich

Reputation: 2204

Hyperlink in a datatemplatecolumn has issues with updating CurrentItem of DataGrid

I have a DataGrid with rows and columns. Each row corresponds to an item in my data collection. For one of my columns, I want to have a hyperlink with text of the name of my data item, and when I click on the hyperlink, it will execute a command for THAT hyperlink's item (the row it is on)

Hope that makes sense so far.

My issue: How do I bind the hyperlink's CommandParameter to use the current row's data item? I'm running into an issue where if I have some random cell selected (say row 3 column 2) and I click a hyperlink in row 1 column 1, it will say the current item is the item in row 3 not row 1!!!

This is my xaml code:

<DataGridTemplateColumn Width="80">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock>
                <Hyperlink Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=DataContext.NavigateToFormCommand}"
                   CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=CurrentItem}">
                    <TextBlock Text="{Binding dataName}" />
                </Hyperlink>
            </TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

If I click the cell the hyperlink is in, and THEN click the hyperlink, everything will work fine. But that's not a really good workflow...if the user clicks the hyperlink it should have the right item...

Upvotes: 0

Views: 353

Answers (1)

ryrich
ryrich

Reputation: 2204

I've solved my own issue, thanks for the help guys (lol)

Anyways, just for the fact that this might help someone later down the road, I didn't use CurrentItem since it doesn't seem to be updating correctly when I click a hyperlink. Instead, I gave the data I needed for the command as a command parameter.

So, the command actually only used the name of my data item, so instead of passing the whole data item (current item), I passed the name instead, and it works fine now.

<Hyperlink Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=DataContext.NavigateToFormCommand}"
                   CommandParameter="{Binding dataName}">

Upvotes: 0

Related Questions