以下是一个示例代码,演示了如何在C#中实现上述需求:

// 在 UserControl 中添加一个 DataGrid 控件和一个按钮控件
// 假设 DataGrid 控件的名称为 dataGrid1,按钮控件的名称为 btnShowDetails

// 定义一个类来表示详细信息
public class DetailedInfo
{
    public string Name { get; set; } 
    public string Age { get; set; } 
    public string Address { get; set; } 
    public string Email { get; set; } 
    public string Phone { get; set; } 
    public string AdditionalInfo { get; set; } 
}

// 在 UserControl 的代码-behind 文件中添加以下代码

private ObservableCollection<DetailedInfo> detailedInfoList;

public UserControl1()
{
    InitializeComponent();
    // 初始化 detailedInfoList
    detailedInfoList = new ObservableCollection<DetailedInfo>();

    // 假设初始展示的五个字段信息是从某个数据源获取的,这里只是示例,你需要根据实际情况进行修改
    List<DetailedInfo> initialInfoList = GetInitialInfoList();

    // 将初始的五个字段信息添加到 detailedInfoList 中
    foreach (DetailedInfo info in initialInfoList)
    {
        detailedInfoList.Add(info);
    }

    // 将 detailedInfoList 设置为 DataGrid 的数据源
    dataGrid1.ItemsSource = detailedInfoList;
}

private void btnShowDetails_Click(object sender, RoutedEventArgs e)
{
    // 获取选中行的详细信息
    DetailedInfo selectedInfo = (DetailedInfo)dataGrid1.SelectedItem;

    // 检查是否有选中行
    if (selectedInfo == null)
    {
        MessageBox.Show('请先选择一行数据!');
        return;
    }

    // 创建一个弹窗窗口
    Window detailsWindow = new Window();
    detailsWindow.Title = '详细信息';
    detailsWindow.Width = 400;
    detailsWindow.Height = 300;

    // 创建一个 StackPanel 用于展示详细信息和填写表单
    StackPanel stackPanel = new StackPanel();
    stackPanel.Margin = new Thickness(10);

    // 创建 TextBlock 用于展示详细信息
    TextBlock infoTextBlock = new TextBlock();
    infoTextBlock.Text = '姓名:' + selectedInfo.Name + '\n年龄:' + selectedInfo.Age + '\n地址:' + selectedInfo.Address + '\n邮箱:' + selectedInfo.Email + '\n电话:' + selectedInfo.Phone;
    stackPanel.Children.Add(infoTextBlock);

    // 创建 TextBox 用于填写额外信息
    TextBox additionalInfoTextBox = new TextBox();
    additionalInfoTextBox.Margin = new Thickness(0, 10, 0, 0);
    stackPanel.Children.Add(additionalInfoTextBox);

    // 创建一个按钮,用于保存填写的额外信息
    Button saveButton = new Button();
    saveButton.Content = '保存';
    saveButton.Margin = new Thickness(0, 10, 0, 0);
    saveButton.Click += (s, args) =>
    {
        // 将填写的额外信息保存到 selectedInfo 对象中
        selectedInfo.AdditionalInfo = additionalInfoTextBox.Text;
        // 关闭弹窗
        detailsWindow.Close();
    };
    stackPanel.Children.Add(saveButton);

    // 将 StackPanel 设置为弹窗窗口的内容
    detailsWindow.Content = stackPanel;

    // 显示弹窗窗口
    detailsWindow.ShowDialog();
}

private List<DetailedInfo> GetInitialInfoList()
{
    // 模拟获取初始的五个字段信息的方法,这里只是示例
    List<DetailedInfo> infoList = new List<DetailedInfo>();
    infoList.Add(new DetailedInfo { Name = 'John', Age = '25', Address = '123 Main St', Email = 'john@example.com', Phone = '123-456-7890' });
    infoList.Add(new DetailedInfo { Name = 'Jane', Age = '30', Address = '456 Elm St', Email = 'jane@example.com', Phone = '987-654-3210' });
    // ...
    return infoList;
}

以上代码示例中,首先在 UserControl 的构造函数中初始化了 detailedInfoList 并将其设置为 DataGrid 的数据源。然后,当点击按钮时,会创建一个弹窗窗口,并在窗口中展示选中行的详细信息和一个用于填写额外信息的文本框。点击保存按钮后,填写的额外信息将保存到选中行的 AdditionalInfo 属性中。

C# UserControl DataGrid 展现部分字段,点击按钮弹窗显示更多详细信息

原文地址: https://www.cveoy.top/t/topic/Hfp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录