现在我要做一个动态生成WPF窗体,想法是替换!
但是却一直无法解决<grid></grid>中动态生成控件并且如何布局的问题!
现在已有的条件就是 我知道有多少个字段和他们的控件类型!
请教各位路过的人帮助!谢谢了!

解决方案 »

  1.   

    在C# code里面不是也可以对Grid进行操作么?
    不过楼主这种情况是应该写一个DataTemplate来展现你的数据。
      

  2.   

    Grid.SetColumn 方法 (System.Windows.Controls) 定义为 
    public static void SetColumn( 
    UIElement element, 
    int value 

      

  3.   

    // Populate XElement contacts with Contacts information.  
                XElement contacts =
                    new XElement("Contacts",
                        new XElement("Contact1",
                            new XElement("Name", "Patrick Hines"),
                            new XElement("Phone", "206-555-0144"),
                            new XElement("Address",
                                new XElement("Street1", "123 Main St"),
                                new XElement("City", "Mercer Island"),
                                new XElement("State", "WA"),
                                new XElement("Postal", "68042")
                            )
                        ),
                        new XElement("Contact2",
                            new XElement("Name", "Yoshi Latime"),
                            new XElement("Phone", "503-555-6874"),
                            new XElement("Address",
                                new XElement("Street1", "City Center Plaza 516 Main St."),
                                new XElement("City", "Elgin"),
                                new XElement("State", "OR"),
                                new XElement("Postal", "97827")
                            )
                        )
                    );            // Create the first TextBlock control.
                // Note that the element has to declare two XAML namespaces.
                XElement textBlock1 = XElement.Parse(
                        @"<TextBlock 
            xmlns='http://schemas.microsoft.com/client/2007' 
            xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' 
            TextWrapping= 'Wrap'
            Width = '400'
            Canvas.Top = '10'
            Text=''/>");            // Get the first child element from contacts xml tree.
                XElement contact1 = contacts.Element("Contact1");            // Set the value of the last attribute "Text"
                // to the content of contacts xml tree.
                textBlock1.LastAttribute.SetValue(contact1.ToString());
                // Get the second child element from contacts xml tree.
                XElement contact2 = contacts.Element("Contact2");            // Create the second TextBlock control.
                // Note that the element has to declare two XAML namespaces.
                XNamespace xmlns = "http://schemas.microsoft.com/client/2007";
                XElement textBlock2 = new XElement(xmlns + "TextBlock",
                    new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
                    new XAttribute("Canvas.Top", 250),
                    new XAttribute("Width", "600"),
                    new XAttribute("Text", contact2.ToString())
                    );
                // Add TextBlock control to the page
                LayoutRoot.Children.Add(XamlReader.Load(textBlock1.ToString()) as UIElement);
                // Add TextBlock control to the page
                LayoutRoot.Children.Add(XamlReader.Load(textBlock2.ToString()) as UIElement);