如何访问 XAML 网格视图的选定行中的信息?
我有一个列表视图,里面有一个网格视图,它绑定到一个自定义对象列表。在一个单元格中,我有三个可以在之间交换的元素,其中之一是带有单击事件的超链接。
当我转到超链接上的单击事件时,如何访问绑定在同一行中的“公司名称”?
这个问题可能与我的 ASP.Net 背景有关 - 我对 WPF 非常陌生。
<GridViewColumn Header="File" Width="100" DisplayMemberBinding="{Binding Path=CompanyFile}"/>
<GridViewColumn Header="Action" Width="300">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Status}" Visibility="{Binding Path=StatusVisibility}"></TextBlock>
<TextBlock Visibility="{Binding Path=ButtonVisibility}"><Hyperlink Click="Hyperlink_Click"><TextBlock Text="{Binding Path=Button}"></TextBlock></Hyperlink></TextBlock>
<ProgressBar Value="{Binding Path=Progress}" Visibility="{Binding Path=ProgressVisibility}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
I have a listview with a gridview inside it and it is bound to a list of custom objects. In one cell I have three elements that I swap between, one of which is a hyperlink with a click event.
How do I get access to the 'CompanyName' that is bound in the same row when I go to the click event on the hyperlink?
This question may bely my ASP.Net background - I am very new to WPF.
<GridViewColumn Header="File" Width="100" DisplayMemberBinding="{Binding Path=CompanyFile}"/>
<GridViewColumn Header="Action" Width="300">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Status}" Visibility="{Binding Path=StatusVisibility}"></TextBlock>
<TextBlock Visibility="{Binding Path=ButtonVisibility}"><Hyperlink Click="Hyperlink_Click"><TextBlock Text="{Binding Path=Button}"></TextBlock></Hyperlink></TextBlock>
<ProgressBar Value="{Binding Path=Progress}" Visibility="{Binding Path=ProgressVisibility}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Hyperlink
继承了父ListViewItem
的DataContext
,因此您只需对其进行强制转换:另一种选择是绑定超链接的
Command
属性传递给DataContext
上的命令,而不是显式处理Click
事件。这有助于将视图与业务相关的代码解耦,并且这是在 MVVM 模式。The
Hyperlink
inherits theDataContext
of the parentListViewItem
, so you just need to cast it:Another option is to bind the hyperlink's
Command
property to a command on yourDataContext
, rather than handling theClick
event explicitly. This helps decoupling the view from the business-related code, and it's the usual way of doing things in the MVVM pattern.