从 DataGrid WPF 将 selectedItem 作为 CommandParameter 传递

发布于 2024-08-12 17:27:15 字数 384 浏览 2 评论 0原文

我有一个数据网格,其中有一个超链接列

当用户单击此超链接时,我想打开一个表单,在其中显示所选员工的工资细目。请让我知道如何将所选记录的 EmployeeId 作为命令参数传递,以便我可以显示详细信息。

I have a Datagrid, which i have a Hyperlink column as

When user clicks on this hyperlink i want to open a form on which i will be showing the salarybreakup for the selected Employee. Please let me know how can i pass the EmployeeId of the selected record as command parameter, so that i can show ite detail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-08-19 17:27:15

我不知道如何使用 DataGrid 来做到这一点,因为我不使用它,但是在 ListBox 中,您可以应用这样的数据模板(删除所有不相关的内容):

<DataTemplate>
    <Grid>
        <Hyperlink Command="{Binding DataContext.MyCommand, 
                             RelativeSource={RelativeSource AncestorType={x:Type ListBox}, 
                             Mode=FindAncestor}}"
                   CommandParameter="{Binding}"/>
    </Grid>
</DataTemplate>

这里我假设您在 ListBox 的 DataContext 中为您的命令拥有 MyCommand 属性。

也对 offtop 感到抱歉,但是您的用户真的需要 DataGrid 及其一排愚蠢的类似 excel 的设计吗?这是显示任何数据的最糟糕的方式。也许使用样式良好的 ListBox 更好?开发人员经常使用 DataGrid,因为它可以轻松显示数据库中某个表的内容。但易于显示和易于使用并不是一回事。因此,如果您不需要 DataGrid 的任何额外功能,请使用其他控件。

I don't know exactly how to do it with DataGrid because I don't use it, But in ListBox you can apply data template like this (everything irrelevant removed):

<DataTemplate>
    <Grid>
        <Hyperlink Command="{Binding DataContext.MyCommand, 
                             RelativeSource={RelativeSource AncestorType={x:Type ListBox}, 
                             Mode=FindAncestor}}"
                   CommandParameter="{Binding}"/>
    </Grid>
</DataTemplate>

Here I suppose that you have MyCommand property for your command in ListBox's DataContext.

Also sorry for offtop, but are your users really need exactly DataGrid with its all-in-one-row silly excel-like design? This is the worst way to display any data. Maybe it's better to use well-styled ListBox? Often developers use DataGrid because its easy do display a content of some table from database. But easy to display and easy to use are not the same things. So, if you don't need any extra functionality of DataGrid, use other controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文