Search code examples
c#xamlbuttoncomboboxdevexpress

Build ComboBox dynamically


So I've build a simple setup with some "GroupBoxes" in which I have some "DimBoxEditors" Now within one of these groups I've also added a "Button" (AddMaterialsBtn) and when this Button is pressed I want to create a new "Groupbox" containing a "ComboBox", I have made this work, the only issue I am facing is the "Orientation" of the created "Groupbox", it keeps creating the new ones Horizontally in the output window and I want it to create Vertically instead. My code for creating the Groupboxes is as follows:

private void AddMaterialsBtn_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        LayoutControl layoutControl = (LayoutControl)layoutItems;
        

        LayoutGroup layoutGroup = new LayoutGroup()
        {
            Name = "Materials" + MaterialButtonCounter,
            Tag = "Materials" + MaterialButtonCounter,                
            IsCollapsible = true,
            MinWidth = 300,
            HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
            Orientation = System.Windows.Controls.Orientation.Vertical,
            View = DevExpress.Xpf.LayoutControl.LayoutGroupView.GroupBox,
        };           

        ComboBox dimBox = new ComboBox()
        {
            Name = "rawMaterials" + MaterialButtonCounter,
            AllowRejectUnknownValues = true,
            AutoComplete = true,
            MaskAutoComplete = DevExpress.Xpf.Editors.AutoCompleteType.Optimistic,
            Width = 145, 
            HorizontalAlignment = System.Windows.HorizontalAlignment.Left                
        };

        LayoutItem layoutItem = new LayoutItem()
        {
            Content = dimBox,
            Name = "RawMaterials" + MaterialButtonCounter,
            HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
            Width = 220
        };

        _ = layoutGroup.Children.Add(layoutItem);
        _ = layoutControl.Children.Add(layoutGroup);
        

        MaterialButtonCounter++;
    }

This works and it creates the Comboboxes perfectly fine, but I cannot make them create Vertically instead of Horizontally.

Sorry if this isn't super well formulated, don't hesitate to ask for any clarification.

Thanks.


Solution

  • Ofcourse the issue is fixed right after I ask the question, don't know if this is useful for anyone but will leave the answer either way.

    So lets say our LayoutControl is called "layoutItems" and the LayoutGroup we want to attach our new groups to is called "testGroup":

    LayoutGorup findGroup = (LayoutGroup)layoutItems.FindName("testGroup");
    

    then you simply add the LayoutGroup you wanna create with the button press to the "findGroup", and then it will create the new groups in the "Orientation" you've defined in the XAML