【问题标题】:WPF MVVM get row object after the DataGridHyperlinkColumn click在 DataGridHyperlinkColumn 单击后 WPF MVVM 获取行对象
【发布时间】:2015-04-03 12:57:00
【问题描述】:

我正在使用 WPF MVVM 模式,并且有一个像这样的 DataGrid:

        <DataGrid Grid.Row="1" Margin="0,10,0,0" AutoGenerateColumns="False" ItemsSource="{Binding Path=displayedResults}">
        <DataGrid.Columns>
            <DataGridTextColumn Width="*" Header="Operation" Binding="{Binding Path=Mode}" />
            <DataGridTextColumn Width="*" Header="Status" Binding="{Binding Path=Result, Converter={StaticResource EnumToString}}" />
            <DataGridHyperlinkColumn Width="100" Header="Details" ContentBinding="{Binding Source={StaticResource DetailsLink}}" />
        </DataGrid.Columns>
    </DataGrid>   

DetailsLink 静态资源表示文本“链接”。当 DataGrid 填满时,每一行都有最后一列带有链接,这将允许用户查看详细信息。要显示详细信息,我需要在单击“链接”链接的行中获取整个对象。

很遗憾,我还没有找到解决方案。可能有一个明显的决定吗? :)

谢谢。

【问题讨论】:

  • 你能分享DetailsLinkxaml吗?

标签: c# .net wpf xaml mvvm


【解决方案1】:

如果您需要传递参数,那么您最好使用ICommand,因此创建一个DataTemplate 而不是使用 HyperLinkColoumn,如下所示:

    <DataGridTemplateColumn Header="Link">
           <DataGridTemplateColumn.CellTemplate>
               <DataTemplate>
                   <TextBlock>
                      <Hyperlink Command="{Binding Path=NavigateToLinkCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                      CommandParameter="{Binding}">
                     <TextBlock Text="{Binding Path=DataContext.Link, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                       </Hyperlink>
                     </TextBlock>
                 </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

现在在您的 ViewModel 中创建一个名为 NavigateToLinkCommand 的命令和一个名为 Link 的字符串属性,当前行 DataContext 将作为您的 Command 的参数传递,并注意您的 AncestorType DataContext ViewModel 设置为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2011-12-25
    • 1970-01-01
    相关资源
    最近更新 更多