简体   繁体   English

数据模板列中的超链接在更新DataGrid的CurrentItem时遇到问题

[英]Hyperlink in a datatemplatecolumn has issues with updating CurrentItem of DataGrid

I have a DataGrid with rows and columns. 我有一个带有行和列的DataGrid 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? 我的问题:如何绑定超链接的CommandParameter以使用当前行的数据项? 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!!! 我遇到一个问题,如果我选择了一些随机单元格(例如第3行第2列),然后单击第1行第1列中的超链接,它将说当前项目是第3行而不是第1行! !

This is my xaml code: 这是我的xaml代码:

<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... 但这不是一个很好的工作流程...如果用户单击超链接,则应具有正确的项目...

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. 无论如何,仅因为这可能对以后的人有所帮助,我没有使用CurrentItem,因为当我单击超链接时,它似乎无法正确更新。 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}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM