How can I access information in the selected row of a XAML gridview?
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&g开发者_高级运维t;
</GridViewColumn.CellTemplate>
</GridViewColumn>
The Hyperlink
inherits the DataContext
of the parent ListViewItem
, so you just need to cast it:
void Hyperlink_Click(object sender, EventArgs e)
{
Hyperlink link = (Hyperlink)sender;
MyCustomObject obj = (MyCustomObject)link.DataContext;
string companyName = obj.CompanyName;
...
}
Another option is to bind the hyperlink's Command
property to a command on your DataContext
, rather than handling the Click
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.
精彩评论