-2

我正在尝试创建一个带有复选标记和 WPF 控件的表格(带有两个文本块的范围滑块)。WPF 控件是使用 Avalon 库编写的,我成功地将它添加到我的表中。但是,当表单关闭时,我需要从文本块中获取文本并将其应用到某个地方。我可以遍历表单上的控件,找到元素宿主,但我不知道如何从表单上的两个文本块中提取值。我的代码附在下面。你能帮我解决这个问题吗?

代码:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public static TableLayoutPanel _T;
        static void set_globalvalue(TableLayoutPanel val)
        {
            _T = val;
        }

        public Form1()
        {
            InitializeComponent();

            TableLayoutPanel TP = new TableLayoutPanel();
            TP.ColumnCount = 2;
            TP.RowCount = 5;
            TP.BackColor = Color.White;
            this.Controls.Add(TP);
            TP.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            int size = 0;
            for (int i = 0; i < TP.RowCount; i++)
            {
                ElementHost host = new ElementHost();
                UserControl1 uc = new UserControl1(1,15);
                host.Name = i.ToString();
                host.Dock = DockStyle.Top;
                host.Child = uc;
                TP.Controls.Add(host, 1, i);
                host.Width = 270;
                host.Height = 40;
                CheckBox ch = new CheckBox();
                ch.Name = i.ToString() + "_che";
                TP.Controls.Add(ch, 0, i);
                size = size + host.Height+20;
            }

            TP.Height = size;
            TP.Width = 700;
            set_globalvalue(TP);
            this.FormClosing += Form1_FormClosing;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            for (int i = 1; i <= _T.ColumnCount; i++)
            {
                for (int j = 0; j <= _T.RowCount; j++)
                {
                    Control c = _T.GetControlFromPosition(i, j);

                    if (c != null & c is System.Windows.Forms.Integration.ElementHost)
                    {
                        ElementHost host =c as ElementHost;
                        System.Windows.UIElement u = host.Child;
                          ???????????????????
                    }
                }
            }
        }
    }
} 
4

1 回答 1

1

获得对元素宿主的引用后,您可以按照以下方式进行操作:

ElementHost host = c as ElementHost;

UserControl1 uc = host.Child as UserControl1;

if (uc != null)
{
   //Get the text from uc
}
于 2012-07-16T17:24:21.373 回答