如何创建一个链接/按钮,允许我在WPF中的DataGrid中打开另一个窗口?

时间:2016-08-17 21:57:53

标签: c# .net wpf datagrid

是否可以在WPF中创建DataGrid并使DataGrid中的一列成为链接/按钮,以便在单击链接/按钮时,这将打开另一个窗口,该窗口将具有基于ID传递的信息通过链接/按钮点击?

我已经在互联网上搜索了两天,但现在运气好了。

这是我的DataGrid。

    <DataGrid Name="tasksgrid" Margin="10,70,0,59"  AutoGenerateColumns="False">
        <DataGrid.Columns>                
            <DataGridTextColumn Header="TASK ID" Binding="{Binding ID}"/>
            <DataGridTextColumn Header="Title" Binding="{Binding TITLE}" Width="250"/>
            <DataGridTextColumn Header="Resource" Binding="{Binding RESOURCE}"/>
            <DataGridTextColumn Header="customer" Binding="{Binding CUSTOMER}"/>
            <DataGridTextColumn Header="REQUESTED DATE" Binding="{Binding DATE_REQUESTED}"/>
            <DataGridTextColumn Header="DUE DATE" Binding="{Binding DUE_DATE}"/>
            <DataGridTextColumn Header="STATUS" Binding="{Binding STATUS}"/>
            <DataGridTextColumn Header="application" Binding="{Binding APPLICATION}"/>
            <DataGridTextColumn Header="REQUESTOR" Binding="{Binding REQUESTOR}"/>
            <DataGridTextColumn Header="Customer ticket" Binding="{Binding CUSTOMER_TICKET_NUM}"/>
            <DataGridTextColumn Header="PT Helpdesk #" Binding="{Binding PT_TICKET_NUM}"/>
            <DataGridTextColumn Header="Vendor Ticket Number" Binding="{Binding EXTERNAL_TICKET_NUM}"/>
            <DataGridTextColumn Header="DESCRIPTION" Binding="{Binding DESCRIPTION}" Width="400"/>               
        </DataGrid.Columns>
    </DataGrid>

以下是DataGrid的填充方式。

WMSTasksDataContext objtaskDataContext = new WMSTasksDataContext();

        tasksgrid.ItemsSource = (from objTasks in objtaskDataContext.WMS_TASKs
                                 select objTasks);

1 个答案:

答案 0 :(得分:0)

所以我做了更多的挖掘,我找到了答案。

XAML代码:

        <DataGrid Name="tasksgrid" Margin="10,70,0,59"  AutoGenerateColumns="False">
        <DataGrid.Columns>               
            <DataGridTemplateColumn Header="Task ID">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate >
                        <TextBlock>
                            <Hyperlink Click="Hyperlink_Click_1" Tag="{Binding Path=ID}">
                                <TextBlock Text="{Binding ID}"></TextBlock>
                            </Hyperlink>
                        </TextBlock>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="TASK ID" Binding="{Binding ID}"/>
            <DataGridTextColumn Header="Title" Binding="{Binding TITLE}" Width="250"/>
            <DataGridTextColumn Header="Resource" Binding="{Binding RESOURCE}"/>
            <DataGridTextColumn Header="customer" Binding="{Binding CUSTOMER}"/>
            <DataGridTextColumn Header="REQUESTED DATE" Binding="{Binding DATE_REQUESTED}"/>
            <DataGridTextColumn Header="DUE DATE" Binding="{Binding DUE_DATE}"/>
            <DataGridTextColumn Header="STATUS" Binding="{Binding STATUS}"/>
            <DataGridTextColumn Header="application" Binding="{Binding APPLICATION}"/>
            <DataGridTextColumn Header="REQUESTOR" Binding="{Binding REQUESTOR}"/>
            <DataGridTextColumn Header="Customer ticket" Binding="{Binding CUSTOMER_TICKET_NUM}"/>
            <DataGridTextColumn Header="PT Helpdesk #" Binding="{Binding PT_TICKET_NUM}"/>
            <DataGridTextColumn Header="Vendor Ticket Number" Binding="{Binding EXTERNAL_TICKET_NUM}"/>
            <DataGridTextColumn Header="DESCRIPTION" Binding="{Binding DESCRIPTION}" Width="400"/>               
        </DataGrid.Columns>
    </DataGrid>

代码背后:

private void Hyperlink_Click_1(object sender, RoutedEventArgs e)
{          
     string taskId = ((Hyperlink)sender).Tag.ToString();

     ViewTask viewTask = new ViewTask();
     viewTask.TaskID.Text = taskId;
     viewTask.Show();
}