【问题标题】:Dynamically add labels and textboxes on the basis of datagridview WPF基于datagridview WPF动态添加标签和文本框
【发布时间】:2021-01-22 07:17:57
【问题描述】:

我是 WPF 新手 实际上,我创建了一个 XML 编辑器,它将浏览一个 xml 文件并将其元素数据加载到数据网格视图中,如下所示enter image description here

但是当我双击数据网格中的任何行时,它需要使用它的值动态填充标签和文本框,如下所示 Capture.PNGenter image description here

帮我解决这个问题

【问题讨论】:

  • 您可以在此处就精确描述的特定问题提出具体问题。但这不是培训委员会。你能告诉我们你的代码中有一个具体的问题吗?

标签: c# wpf wpf-controls


【解决方案1】:

为了在双击数据网格时动态填充文本块或标签,您可以: -首先将事件添加到 xaml 中的数据网格:

<DataGrid x:Name="datagrid" MouseDoubleClick="datagrid_MouseDoubleClick"></DataGrid>

-然后检索事件中的数据,以便在标签或文本块中显示它们:

        private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            YourClass SelectedItem = (YourClass)datagrid.SelectedItem;
            if(SelectedItem != null)
            {
                textblock1.Text = SelectedItem.name;
                textblock2.Text = SelectedItem.id.ToString();
                label1.Text = SelectedItem.branch;
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多