WPF,WinForm C#

时间:2012-07-16 16:53:34

标签: c# wpf avalondock

我正在尝试创建一个带有复选标记和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;
                          ???????????????????
                    }
                }
            }
        }
    }
} 

1 个答案:

答案 0 :(得分:1)

一旦你有了元素主机的引用,你就可以沿着这些方向做点什么:

ElementHost host = c as ElementHost;

UserControl1 uc = host.Child as UserControl1;

if (uc != null)
{
   //Get the text from uc
}