Reputation: 10648
I have a WPF MVVM application which in its main form has a datagridview. This datagrid has some columns. One of them is of type DataGridHyperlinkColumn.
In the example below I will simplify the real case to focus on the interesting part that is important for me, so here I put an example with a datagrid with only one column of type DataGridHyperlinkColumn. Also my model is simplified here.
<Window x:Class="DataGridExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="MainWindow" Height="350" Width="525">
<Grid>
<my:DataGrid x:Name="MyDataGrid"
ItemsSource="{Binding Path=MyListOfItems}"
AutoGenerateColumns="False">
<my:DataGrid.Columns>
<my:DataGridHyperlinkColumn Header="LinkColumn"
Binding="{Binding myExtraData.LinkName}"
ContentBinding="{Binding myExtraData.LinkValue}">
<my:DataGridHyperlinkColumn.ElementStyle>
<Style TargetType="TextBlock">
<EventSetter Event="Hyperlink.Click" Handler="OnCellHyperlinkClick" />
</Style>
</my:DataGridHyperlinkColumn.ElementStyle>
</my:DataGridHyperlinkColumn>
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>
MyListOfItems is a public property in my view model which gets/sets a list of items:
List<MyDataModel> myListOfItems
Model: My model contains a nested class MyExtraData.
public class MyDataModel
{
public MyExtraData myExtraData{ get; set; }
public MyDataModel()
{
myExtraData= new MyExtraData("Click here!", "Hi! How are you?");
}
}
public class MyExtraData
{
private string linkName= string.Empty;
private string linkValue= string.Empty;
public string LinkName
{
get
{
return this.linkName;
}
private set
{
if (!this.linkName.Equals(value))
{
this.linkName= value;
}
}
}
public string LinkValue
{
get
{
return this.linkValue;
}
private set
{
if (!this.linkValue.Equals(value))
{
this.linkValue = value;
}
}
}
public MyExtraData(string linkName, string linkValue)
{
this.LinkName = linkName;
this.LinkValue = linkValue;
}
}
Finally in the view codeBehind, here the handler:
private void OnCellHyperlinkClick(object sender, RoutedEventArgs e)
{
var rowData = ((Hyperlink)e.OriginalSource).DataContext as MyExtraData;
}
My problem is in OnCellHyperlinkClick event. When I convert into MyExtraData then rowData contains a null value. From within this event I would like to obtain the value for the link, that is, contentBinding, and then open a custom window and pass this value to it, I mean, I want to pass the value "Hi! How are you?" to my custom window.
Note that for this example, all the column's cells have the same name and value for the link. In my real case, all the column's cells have the same name for the link, for example, "Click here!" but different value for it.
Upvotes: 0
Views: 1276
Reputation: 10648
The problem was that I was converting to an object that does not match the type of DataContext. There are two solutions here:
private void OnCellHyperlinkClick(object sender, RoutedEventArgs e)
{
string linkValue =((MyDataModel)((TextBlock)sender).DataContext).myExtraData.LinkValue;
// pass linkvalue to my custom WPF window
}
or:
private void OnCellHyperlinkClick(object sender, RoutedEventArgs e)
{
MyExtraData myExtraData = (((Hyperlink)e.OriginalSource).DataContext as MyDataModel).myExtraData
string linkValue = myExtraData.LinkValue;
// pass linkvalue to my custom WPF window
}
Basically is the same, I do not see the difference between using sender or e.OriginalSource.
Upvotes: 1