问题
I am trying to create a hypelink which the user can click and navigate to a website.
The link is working but I am getting this exception which stops the application: Failed to convert resource into object.
The hyperlink will be a part og a datagrid. Here is what I have:
XAML:
...
<DataTemplate x:Key="hyperlinkTemplate">
<TextBlock>
<Hyperlink NavigateUri="{Binding Link}" RequestNavigate="dataLink_RequestNavigate">
<TextBlock Text="{Binding TaskID}"></TextBlock>
</Hyperlink>
</TextBlock>
</DataTemplate>
...
<DataGrid Grid.Column="1" AutoGenerateColumns="False" ItemsSource="{Binding Tasks}" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name width link" CellTemplate="{StaticResource hyperlinkTemplate}"></DataGridTemplateColumn>
...
==================================================================
Code behind:
...
private void dataLink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
Process.Start(e.Uri.ToString());
e.Handled = true;
}
...
==================================================================
Class data: Link
public string Link
{
get { return link; }
set
{
link = value;
Notify("Link");
}
}
==================================================================
Task class
[Serializable]
public class Task
{
private XmlNode node;
private string category = "";
private int taskID = -1;
private string taskName = "";
private string taskResponsible = "";
private string taskResponsibleDepartment = "";
private int priority = 5;
private string status = "Unknown";
private string predecessorIndices = "None";
private int indentLevel = 0;
private int sortOrder = 0;
private DateTime startDate = DateTime.Now;
private TimeSpan estimatedHours = default(TimeSpan);
private TimeSpan actualHours = default(TimeSpan);
private DateTime estimatedDeploymentDate = default(DateTime);
private DateTime desiredImplementationDate = default(DateTime);
private string estimatedHoursRecovery = "";
private string actualHoursRecovery = "";
private string tags = "";
private TimeSpan totalHoursActual = default(TimeSpan);
private Department iN = new Department();
private Department aPP = new Department();
private Department sIS = new Department();
private string link = "";
...
==================================================================
Do you have any suggestions?
BR
回答1:
Found out myself. I needed to add e.handled = true;
to the datagrid_RequestNavigate. Just updated the code examples too.
It is all working now
来源:https://stackoverflow.com/questions/7888871/hyperlink-keeps-failing