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

本文关键字:WPF 允许我 窗口 另一个 DataGrid 按钮 创建 何创建 一个 链接 | 更新日期: 2023-09-27 18:02:55

是否有可能在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);

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

所以我做了更多的调查,我找到了答案。

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();
}