超链接一直在失败

时间:2011-10-25 11:54:10

标签: c# wpf hyperlink datagridtemplatecolumn

我正在尝试创建一个用户可以点击并导航到网站的超链接。

该链接正在运行,但我收到此异常会停止该应用程序: 无法将资源转换为对象。

超链接将是数据网格的一部分。这就是我所拥有的:

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>
...

=============================================== ===================

代码背后:

...
private void dataLink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
    Process.Start(e.Uri.ToString());
    e.Handled = true;
}
...

=============================================== ===================

班级数据:链接

public string Link
    {
        get { return link; }
        set
        {
            link = value;
            Notify("Link");
        }
    }

=============================================== ===================

任务类

[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 = "";
...

=============================================== ===================

你有什么建议吗?

BR

1 个答案:

答案 0 :(得分:3)

发现自己。我需要将e.handled = true;添加到datagrid_RequestNavigate。刚刚更新了代码示例。

现在全部正常工作